note 23825 added to function.preg-replace
| From: | maxim@php.net | Date: | Mon, 29 Jul 2002 18:03:48 +0000 |
| Subject: | note 23825 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-33882@lists.php.net to get a copy of this message | ||
Here's a valuable example of Array usage with /e modifier:
error_reporting ( E_ALL | E_NOTICE );
$name = Array(
'first' =>'Maxim'
,'middle' =>'Olegovich'
,'last' =>'Maletsky'
,'email' =>'maxim@php.net'
);
$str = '<a href="mailto:$email">$first
($middle) $last</a>';
$out = preg_replace (
"/(\\$(\w+))/e",
"@\$name['\\2']",
$str
);
echo "
$str
becomes
$out
<br>
";
outputs :
<a href="$email">$first ($middle) $last</a>
becomes
<a href="mailto:maxim@php.net">Maxim
(Olegovich) Maletsky</a>
<br>
NOTE: the 'at' (@) is optional. This is to prevent errors in case of the error reporting
as above. You may as well avoid it if running with different error reporting level.
Another neat idea could be a better regular expression that supports backslashes (\) infront the
sollar sign ($) for the variable. But, that is another story.
-maxim maletsky
--
http://www.php.net/manual/en/function.preg-replace.php
http://master.php.net/manage/user-notes.php?action=edit+23825
http://master.php.net/manage/user-notes.php?action=delete+23825
http://master.php.net/manage/user-notes.php?action=reject+23825