note 28794 added to function.preg-replace

From: Date: Fri, 24 Jan 2003 02:18:21 +0000
Subject: note 28794 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-42671@lists.php.net to get a copy of this message
For those having problems using replacements with the $ or \ characters in them, try this function... function preg_replacement_quote($replacement) { $replacement = str_replace("\\", "\\\\", $replacement); $replacement = str_replace("$", "\\$", $replacement); return $replacement; } example: $text = "xyz salary abc"; $search = "/salary/"; $replacement = "$1,000,000 dollars"; echo preg_replace($search, $replacement, $text); // BAD echo preg_replace($search, preg_replacement_quote($replacement), $text); // GOOD Whenever you use data from user input or databases as a replacement value, you should use this function. -- http://www.php.net/manual/en/function.preg-replace.php http://master.php.net/manage/user-notes.php?action=edit+28794 http://master.php.net/manage/user-notes.php?action=delete+28794 http://master.php.net/manage/user-notes.php?action=reject+28794

« previous php.notes (#42671) next »