note 84058 added to function.preg-replace
| From: | patrick dot d dot hayes at gmail dot com | Date: | Wed, 25 Jun 2008 21:15:44 +0000 |
| Subject: | note 84058 added to function.preg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-140820@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 66.207.199.35
Probable Submitter: 24.68.252.93
----
Manual Page -- http://www.php.net/manual/en/function.preg-replace.php
Edit -- https://master.php.net/note/edit/84058
Del: integrated -- https://master.php.net/note/delete/84058/integrated
Del: useless -- https://master.php.net/note/delete/84058/useless
Del: bad code -- https://master.php.net/note/delete/84058/bad+code
Del: spam -- https://master.php.net/note/delete/84058/spam
Del: non-english -- https://master.php.net/note/delete/84058/non-english
Del: in docs -- https://master.php.net/note/delete/84058/in+docs
Del: other reasons-- https://master.php.net/note/delete/84058
Reject -- https://master.php.net/note/reject/84058
Search -- https://master.php.net/manage/user-notes.php