note 35286 added to function.preg-replace-callback
| From: | matthew at de-construct dot com | Date: | Tue, 26 Aug 2003 15:48:52 +0000 |
| Subject: | note 35286 added to function.preg-replace-callback | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55115@lists.php.net to get a copy of this message | ||
I spent ages trying to work out how to send additional data in to a callback function without using
a global (i don't like globals ;) and you can do it using serialize() and create_function()
<?
$outer_data = array('foo'=>'abc','bar'=>'123');
$callback = create_function('$matches',
'
$inner_data = unserialize("'. addslashes(serialize($outer_data)) .'");
// do something here
return $inner_data[$matches[1]];
'
);
$template_data = preg_replace_callback("/\{[$](.*?)\}/", $callback, $template_data);
?>
so in the above example, if your regexp is like:
s/(foo|bar)ing/
it will appear:
for "fooing": abcing
for "baring": 123ing
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35286
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35286&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35286&report=yes