note 56466 added to function.func-get-args
| From: | php-general at lists dot php dot net | Date: | Sat, 03 Sep 2005 17:24:13 +0000 |
| Subject: | note 56466 added to function.func-get-args | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94697@lists.php.net to get a copy of this message | ||
Here's a simple, recursive method for grabbing text arguments from "xyz=1234", even
if they're in an array. It uses any text without an equal sign as a key, setting that to true.
<?php
// Not binary safe
function text_arg_parse ($input) {
// Check if there are more than one arguments or input isn't an array. In that case use
func_get_args();
if (func_num_args() > 1 || !is_array($input)) $input = func_get_args();
for ($i = 0; $i < count($input); $i++) {
// If the entry is an array, send it back to be processed and add to the $output array
if (is_array($input[$i]))
array_push($output, text_arg_parse($input[$i]));
// If the entry has an equal sign, use everything before the '=' as a key, everything
after as data
else if (($pos = strpos($input[$i], '=')) !== FALSE )
$output[trim(substr($input[$i], 0, $pos))] = trim(substr($input[$i], $pos + 1));
// If the entry has no equal sign, use it as a key for a new entry and set it to "TRUE"
else $output[$input[$i]] = TRUE;
}
return $output;
}
?>
It can be used as such:
<?php
$a = text_arg_parse ('heya=hello', 'two', 'three', 'there=not
here');
print_r($a);
?>
which outputs:
Array
(
[heya] => hello
[two] => 1
[three] => 1
[there] => not here
)
----
Manual Page -- http://www.php.net/manual/en/function.func-get-args.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56466
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56466&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56466&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56466&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56466&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56466&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56466&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56466&report=yes
Search -- http://master.php.net/manage/user-notes.php