Forum Moderators: coopster

Message Too Old, No Replies

Why no reverse to the 'parse_str' function?

Very odd, that there is no function that implodes variables for URL

         

MTKilpatrick

11:10 am on Jul 14, 2003 (gmt 0)

10+ Year Member



Folks,

am I missing something, or is there really no built in function that performs the reverse of parse_str? You can parse a string to extract variables in the manner that $_GET extracts the variables from the URL query string...

...but why does there appear to be no reverse procedure? If you're managing webpage with PHP and there are mutliple variables in the URL, it's handy to have an easy way to generate the URL for links, etc.

I'm really surprised no such function exists. I had to write one by hand - and that only works for simple variables, not arrays. Function as below, assuming $args is an array like $_GET.

function make_url ($args)
{
$temp = array();
foreach ($args as $key => $value)
{
$temp[] = $key."=".$value;
}
$url = $_SERVER["PHP_SELF"] . "?" . implode("&", $temp);
return $url;
}

Michael

hakre

2:15 pm on Jul 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi michael, welcome to webmasterworld [webmasterworld.com].

i never thought about it, thanks for your posting. i haven't researched yet if there is a function already for this, but in yours, don't forget to urlencode the variables in the query string:

$temp[] = $key."=".urlencode($value);

- hakre.

MTKilpatrick

2:26 pm on Jul 14, 2003 (gmt 0)

10+ Year Member



After a bit of hunting around I found a better version written by someone and posted on a public site. It's better than mine in that it doesn't print an initial "?" if there are no arguments at all in the array of commands £args. It uses the? switch within a string concatenation so that the first character is a "?" but once the string has a positive length, the ampersand is used:

str = '';
foreach ($args as $key => $value)
{
$str .= (strlen($str) < 1)? "?" : "&";
$str .= $key . '=' . rawurlencode($value);
}

then just append that to the end of PHP_SELF.

With regard to the urlencode stuff, I assume I never need that if I know that values are always going to be integer numbers?

Michael

hakre

8:37 am on Jul 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




With regard to the urlencode stuff, I assume I never need that if I know that values are always going to be integer numbers?

if you know for shure the values are going to be integer (or only contain chars with no need to be escaped), then you don't need to urlencode. but for a robust function, it's ever good to have it in.

vars in php can be very tricky, because they are not bound to any type, they can be a number (int) or a string the same time, urlencode will make the function save for any case.

yeah to make it 100% perfect you should even check if its a number or a string because arrays won't work here anyway.

so the only left question is, how to handle arrays in that function ;).

- hakre.

MTKilpatrick

11:24 am on Jul 15, 2003 (gmt 0)

10+ Year Member



Just for the mental exercise I think I might have a go at writing a function that will handle arrays too. I don't think it will be *too* difficult. I don't think you can, for example, have arrays of arrays within the query_string (without any documentation as to that effect I tried to guess the syntax but couldn't get parse_str to do anything), so the extent of the possible variations in the structure of the string is not *too* great.

Michael