Last active
August 29, 2015 14:26
-
-
Save sherbrow/fb1495dfd2eb9954df29 to your computer and use it in GitHub Desktop.
Form hidden fields equivalent of http_build_query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* First draft of sister function http_build_query http://php.net/manual/function.http-build-query.php | |
* intended to generate hidden form inputs from the *same* arguments | |
*/ | |
function html_build_form(array $array, $prefix = '') { | |
$str = ''; | |
foreach($array as $key => $val) { | |
$name = $prefix?$prefix.'['.$key.']':$key; | |
if(is_array($val)) $str .= html_build_form($val, $name); | |
else { | |
$str .= '<input type="hidden" name="'.htmlentities($name).'" value="'.htmlentities($val).'">'; // use htmlentities($name,ENT_COMPAT,'UTF-8'); if server encoding differs from text | |
} | |
} | |
return $str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment