note 94750 added to functions.arguments

From: Date: Mon, 23 Nov 2009 00:38:58 +0000
Subject: note 94750 added to functions.arguments
Groups: php.notes 
Request: Send a blank email to php-notes+get-163178@lists.php.net to get a copy of this message
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 //////////////////////////////////////////////// ---- Server IP: 69.147.83.197 Probable Submitter: 76.7.91.106 ---- Manual Page -- http://www.php.net/manual/en/functions.arguments.php Edit -- https://master.php.net/note/edit/94750 Del: integrated -- https://master.php.net/note/delete/94750/integrated Del: useless -- https://master.php.net/note/delete/94750/useless Del: bad code -- https://master.php.net/note/delete/94750/bad+code Del: spam -- https://master.php.net/note/delete/94750/spam Del: non-english -- https://master.php.net/note/delete/94750/non-english Del: in docs -- https://master.php.net/note/delete/94750/in+docs Del: other reasons-- https://master.php.net/note/delete/94750 Reject -- https://master.php.net/note/reject/94750 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163178) next »