note 30822 added to function.preg-replace

From: Date: Mon, 31 Mar 2003 16:17:40 +0000
Subject: note 30822 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-46014@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/function.preg-replace.php http://master.php.net/manage/user-notes.php?action=edit+30822 http://master.php.net/manage/user-notes.php?action=delete+30822 http://master.php.net/manage/user-notes.php?action=reject+30822

« previous php.notes (#46014) next »