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

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

« previous php.notes (#109667) next »