note 26736 deleted from function.preg-replace-callback by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 12:26:38 +0000 |
| Subject: | note 26736 deleted from function.preg-replace-callback by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57456@lists.php.net to get a copy of this message | ||
Note Submitter: river@dialling.spamfree.info
----
The callback function is called once for each match, and should return the replacement value for
that match. The array passed to the callback function will only have a single element unless you
have used ( ) in the match pattern.
Example: find keywords introduced by `` and replace with values from table $val
$val[bird]='cormorant';
$val[bird2]='shag';
$val[nest]='paper bag';
$sample = 'The common `
bird or bird2, lays its eggs in a `nest';
$result = preg_replace_callback('/``(\w+)/', lookup, $sample);
echo $sample,"\n<hr>\n",$result;
function lookup($items) {
global $val;
return $val[$items[1]]; #first ( ) in pattern
}