note 78071 deleted from function.preg-replace-callback by felipe

From: Date: Sun, 23 Mar 2008 23:02:21 +0000
Subject: note 78071 deleted from function.preg-replace-callback by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-137410@lists.php.net to get a copy of this message
Note Submitter: vinyanov at poczta dot onet dot pl ---- If you want to replace your matches with a unique calculation and you do not want to use: 1. preg_replace() and /e flag* 2. preg_replace_callback() and create_function()** 3. preg_replace_callback() and fill the namespace with an additional function You can insert both call and solution within one function. Then, you may supply callback with the __FUNCTION__ predefined constant, and the function will separate its responsibilities itself by the type of the argument. Since preg_replace_callback() returns an array, this is easy. Quite useless example: <?php function debug($s) { if(is_string($s)) { return preg_replace_callback('/{\?(cl|co|f|i|v)}/', __FUNCTION__, $s); } foreach(array( 'co' =>'nstants', 'f' =>'unctions', 'v' =>'ars', 'cl' =>'asses', 'i' =>'nterfaces', ) as $p[1] => $p[2]) { if($p[1] === $s[1]) { $p[0] = in_array($p[1], array('cl', 'i')) ? 'declared' : 'defined'; $f = vsprintf('get_%3$s_%2$s%1$s', $p); return var_export($f(), 1); } } } echo debug('Variables: {?v} Nothing: {?n} Interfaces: {?i}'); ?> * I do not know whether it evokes eval() or not ** http://tinyurl.com/3azwyy

« previous php.notes (#137410) next »