note 28794 deleted from function.preg-replace by sander

From: Date: Sat, 15 Feb 2003 15:23:22 +0000
Subject: note 28794 deleted from function.preg-replace by sander
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-43988@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.

« previous php.notes (#43988) next »