note 56472 deleted from function.func-get-args by bjori

From: Date: Tue, 25 Apr 2006 19:06:05 +0000
Subject: note 56472 deleted from function.func-get-args by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-109666@lists.php.net to get a copy of this message
Note Submitter: ti3x at yahoo dot ca ---- 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.

« previous php.notes (#109666) next »