note 56472 added to function.func-get-args
| From: | ti3x at yahoo dot ca | Date: | Sat, 03 Sep 2005 23:05:32 +0000 |
| Subject: | note 56472 added to function.func-get-args | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94704@lists.php.net to get a copy of this message | ||
This is an update to what I posted earlier today. It's a function that is useful to me, but
it's rather situational (and far from the most flexible thing I've ever written). Anyway,
this update is to include the ability to deal with successive arrays properly, as the previous one
broke down after some testing.
A few notes:
* Any numerical keys which also have numerical values will be removed.
* Anything before the first '=' sign will have trim() applied and be turned into a key.
Everything after the '=' sign will be turned into the value.
* If there is no '=' sign and the key is a numeric value, that value will be turned into
the key and the value will be set to true.
* If the array entry does have a key, both key and value will be copied over without any change
(except in the case of an array entry)
<?php
// text_arg_parse
// Parse a text argument, or an array with text.
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();
$keys = array_keys($input);
$input = array_values($input);
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])) {
$insert = text_arg_parse($input[$i]);
$insertKey = array_keys($insert);
$insert = array_values($insert);
for ($n = 0; $n < count($insert); $n++)
$output[$insertKey[$n]] = $insert[$n];
}
// Transfer non-integer keys to $output.
else if (!is_int($keys[$i]))
$output["$keys[$i]"] = $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", unless the value and key are both integers
else if (!(is_int($keys[$i]) && is_int($input[$i]))) $output[$input[$i]] = TRUE;
}
return $output;
}
$arr = array("a=First Letter", six => 'Number 6', 'True');
$text = text_arg_parse ('6', 10, 'two =Second Number ', $arr, '1 is
equal to');
echo "Printing \$text: ";
print_r ($text);
?>
This will output:
Printing $text: Array
(
[6] => 1
[two] => Second Number
[a] => First Letter
[six] => Number 6
[True] => 1
[1 is equal to] => 1
)
I'll try to streamline this a bit more, but it works quite well as is.
----
Manual Page -- http://www.php.net/manual/en/function.func-get-args.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56472
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56472&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56472&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56472&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56472&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56472&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56472&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56472&report=yes
Search -- http://master.php.net/manage/user-notes.php