note 78071 added to function.preg-replace-callback

From: Date: Wed, 26 Sep 2007 22:34:52 +0000
Subject: note 78071 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-130812@lists.php.net to get a copy of this message
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 ---- Server IP: 195.136.184.34 Probable Submitter: 83.31.206.21 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- https://master.php.net/note/edit/78071 Del: integrated -- https://master.php.net/note/delete/78071/integrated Del: useless -- https://master.php.net/note/delete/78071/useless Del: bad code -- https://master.php.net/note/delete/78071/bad+code Del: spam -- https://master.php.net/note/delete/78071/spam Del: non-english -- https://master.php.net/note/delete/78071/non-english Del: in docs -- https://master.php.net/note/delete/78071/in+docs Del: other reasons-- https://master.php.net/note/delete/78071 Reject -- https://master.php.net/note/reject/78071 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130812) next »