List Info

Thread: note 78071 added to function.preg-replace-callback




note 78071 added to function.preg-replace-callback
user name
2007-09-26 17:34:52
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-replac
e-callback.php
Edit        -- https://master
.php.net/note/edit/78071
Del: integrated  -- h
ttps://master.php.net/note/delete/78071/integrated
Del: useless     -- http
s://master.php.net/note/delete/78071/useless
Del: bad code    -- htt
ps://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     -- http
s://master.php.net/note/delete/78071/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78071
Reject      -- https://mast
er.php.net/note/reject/78071
Search      -- https://
master.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )