note 35286 deleted from function.preg-replace-callback by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 12:25:34 +0000 |
| Subject: | note 35286 deleted from function.preg-replace-callback by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57448@lists.php.net to get a copy of this message | ||
Note Submitter: matthew@de-construct.com
----
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