Reputation: 3483
I'm making a PHP function that converts a $_GET array into the URL string format..
e.g. Array('key1'=>'value1', 'key2'=>'value2')
gets converted to: ?key1=value1&key2=value2
I think the function it's doing its work correctly. But when I echo the result, in the HTML page all instances of "&
" are replaced by "&
". So, the conversion in the browser ends up being ?key1=value1&key2=value2
.
Here's my PHP function:
/**
*
* @param Array $GETArray Pass in the associative $_GET array here.
* @return string The $GETArray converted into ?key=value&key2=value2&... form.
*/
function strGET($GETArray) {
if (sizeof($GETArray) < 1) {
return '';
}
$firstkey = key($GETArray);
$firstvalue = $GETArray[$firstkey];
$sofar = "?$firstkey=$firstvalue";
array_shift($GETArray);
foreach ($GETArray as $key => $value) {
$sofar .= '&'."$key=$value";
}
return $sofar;
}
Upvotes: 2
Views: 3053
Reputation: 156
Use http://us.php.net/manual/en/function.http-build-query.php function instead.
Upvotes: 0
Reputation: 522175
Sidestepping your question, but use http_build_query
. It does exactly that.
Also, your function does not HTML-escape anything. Neither does PHP by itself. You must be escaping it somewhere. Which, BTW, is correct. Ampersands should be escaped.
Upvotes: 4