note 84058 deleted from function.preg-replace by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Nov 2008 19:46:13 +0000 |
| Subject: | note 84058 deleted from function.preg-replace by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147137@lists.php.net to get a copy of this message | ||
Note Submitter: patrick dot d dot hayes at gmail dot com
----
Here's some handy-dandy code that will properly escape a string that includes regex special
characters.
<?php
$x = "
/\\[|\\\\|\\^|\\$|\\.|\\||\\?
|\\*|\\+|\\(|\\)|\\{|\\}/
";
$escaped_string = preg_replace($x, "\\\\$0", $in_string);
?>
This is useful if you are generating dynamic regular expressions based on user input. With the
above example, the $escaped_string variable is now ready to be used in another regular expression as
the search variable.