note 72911 added to function.http-build-query
| From: | mqchen at gmail dot com | Date: | Sat, 03 Feb 2007 21:27:27 +0000 |
| Subject: | note 72911 added to function.http-build-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122771@lists.php.net to get a copy of this message | ||
To flyingmeteor,
Your function is pleasingly adequate, however, when comparing the results from your function with
the results from the actual function, it has a minor defect. If one uses a special character (e.g.
øæåöä) in a key in the $data array, it is not encoded by your function, but it is by
the actual one.
This is easily solved by some minor edits:
<?php
if(!function_exists('http_build_query')) {
function http_build_query($data,$prefix=null,$sep='',$key='') {
$ret = array();
foreach((array)$data as $k => $v) {
$k = urlencode($k);
if(is_int($k) && $prefix != null) {
$k = $prefix.$k;
};
if(!empty($key)) {
$k = $key."[".$k."]";
};
if(is_array($v) || is_object($v)) {
array_push($ret,http_build_query($v,"",$sep,$k));
}
else {
array_push($ret,$k."=".urlencode($v));
};
};
if(empty($sep)) {
$sep = ini_get("arg_separator.output");
};
return implode($sep, $ret);
};
};
?>
Forgive my personal coding standard.
----
Server IP: 62.73.196.231
Probable Submitter: 193.216.225.44
----
Manual Page -- http://www.php.net/manual/en/function.http-build-query.php
Edit -- https://master.php.net/note/edit/72911
Del: integrated -- https://master.php.net/note/delete/72911/integrated
Del: useless -- https://master.php.net/note/delete/72911/useless
Del: bad code -- https://master.php.net/note/delete/72911/bad+code
Del: spam -- https://master.php.net/note/delete/72911/spam
Del: non-english -- https://master.php.net/note/delete/72911/non-english
Del: in docs -- https://master.php.net/note/delete/72911/in+docs
Del: other reasons-- https://master.php.net/note/delete/72911
Reject -- https://master.php.net/note/reject/72911
Search -- https://master.php.net/manage/user-notes.php