Forum Moderators: coopster

Message Too Old, No Replies

Why no reverse to the 'parse str' function? (revived)

parse_str reverse function in php

         

kryo

8:06 am on Feb 26, 2009 (gmt 0)

10+ Year Member



Hi there, I stumbled on this site looking for a reverse to the parse_str function in php. I saw a very old message that did have a working function, but I needed something slightly more sophisticated (that could support arrays). So, here's my solution written in PHP.

This is a function the php guys should implement internally for performance, but this function does the job fine:


function full_url_encode($params) {
$result = "";
if(is_array($params)) {
foreach($params as $key=>$value) {
if(is_array($value)) {
foreach($value as $vkey=>$vvalue) {
$result .= sprintf('%s[]=%s&', $key, urlencode(stripslashes($vvalue)));
}
}
else {
$result .= sprintf('%s=%s&',$key,urlencode(stripslashes($value)));
}
}
$result = preg_replace('/\&$/','',$result);
}
else {
$result = urlencode(stripslashes($params));
}
return $result;
}

Related to a different topic, another missing php function is something like this.


function array_get($array, $key, $default = null) {
if (! isset ( $array [$key] )) {
return $default;
}
return $array [$key];
}

whoisgregg

2:33 pm on Feb 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, kryo!

Thanks for posting those functions. :)

I think that http_build_query [php.net]() will reverse a parse_str as long as you used the optional array variable.

kryo

2:58 am on Feb 27, 2009 (gmt 0)

10+ Year Member



hey thanks for that didn't realize that existed... it's a new(ish) php5 function. Seems it's doing the same thing, I guess it's better to use because its an internal php5 function. But still for those of you still on php4, the above should work nicely.

thanks again

-k