note 94750 deleted from functions.arguments by danbrown
| From: | danbrown@php.net | Date: | Mon, 23 Nov 2009 15:34:56 +0000 |
| Subject: | note 94750 deleted from functions.arguments by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163197@lists.php.net to get a copy of this message | ||
Note Submitter: Jeff
----
allankelly writes:
I like to pass an associative array as an argument.
While this is not exactly what he asks for, what I use is:
/////// Permits HTML type attributes / arguments for PHP function calls ///////
////// usage:
//// function foo () {
//// $prompt = "This is multiple words";
//// $color = 'green'; // default value
//// $width =''; // all allowable attributes must be defined,
//// all attributes are converted to lowercase, values are mixed, as is.
//// eval(GET_ATTRIBS);
//// ....
//// }
////// < ? php foo("width = 7 color = red prompt = 'More Words'") ? >;
////// NO SPACES in PHP prefix & postfix brackets
////// "Just like HTML," with longer prefix & postfix brackets,
////// an extra pair of (), and extra pair of quotes.
////// Advantage over straight function call:
////// all attributes are non positional, all optional
////// Function and attribute names are case insensitive.
////// Attribute values case is kept.
if(!defined('GET_ATTRIBS'))
define('GET_ATTRIBS',
'@extract(@func_get_attributes(func_get_arg(0)), EXTR_IF_EXISTS);');
if(!function_exists('func_get_attributes')) {
function func_get_attributes() {
$a = trim(func_get_arg(0));
for($j = 0; $a != ''; $j++) {
$aa = explode('=', $a, 2);
$k[$j] = strtolower(trim($aa[0]));
$aa[1] = trim($aa[1]);
$fc = substr($aa[1],0,1);
if($fc == '"' or $fc == "'") {
for ($i = 1; $i < strlen($aa[1]); $i++) { //>
$nc = substr($aa[1],$i,1);
if ($nc == '\\') {
if (substr($aa[1],$i+1,1) == $fc) $aa[1] = substr($aa[1],0,$i) . substr($aa[1],$i+1);
continue;
}
if ($nc == $fc) {
$aa[1] = substr($aa[1],0,$i) . substr($aa[1],$i+1);
$i--;
break;
}
}
$v[$j] = substr($aa[1], 1, $i);
$a = substr($aa[1], $i+2);
}
else {
$t = explode(' ',$aa[1],2);
$v[$j] = trim($t[0]);
$a = trim($t[1]);
}
}
for($i = 0; $i < sizeof($k); $i++) { //>
$r[$k[$i]] = $v[$i];
}
return $r;
}
}
///// Jeff Hennick, 2005
////////////////////////////////////////////////