note 30822 deleted from function.preg-replace by sniper
| From: | sniper@php.net | Date: | Sun, 07 Sep 2003 22:01:50 +0000 |
| Subject: | note 30822 deleted from function.preg-replace by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-55685@lists.php.net to get a copy of this message | ||
Note Submitter: checat@chat.ru
----
It should be noted in the manual, that when backreference substitution is not desired, but
$replacement text can contain backslashes and dollar signs, you should ALWAYS use an additional
function call to ADD SLASHES TO REPLACEMENTS.
$replace='I have $350 in my hand!';
$text="He says, 'TEXT'";
echo preg_replace('|TEXT|',$replace,$text);
will output
He says, 'I have 0 in my hand!'
$35 is counted as backreference and is substituted with null string.
But:
echo preg_replace('|TEXT|',addcslashes($replace,'\\$'),$text);
He says, 'I have $350 in my hand!'
That call to addcslashes() converts single backslash to double backslash and dollar to
backslash-dollar, preg_replace() always strips additional slashes only from \ and $ in $replacement.