note 59533 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 19:54:03 +0000 |
| Subject: | note 59533 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127676@lists.php.net to get a copy of this message | ||
Note Submitter: jcheger at acytec dot com
----
Escaping quotes may be very tricky. Magic quotes and preg_quote are not protected against double
escaping. This means that an escaped quote will get a double backslash, or even more. preg_quote
("I\'m using regex") will return "I\\'m using regex".
The following example escapes only unescaped single quotes:
<?php
$a = "I'm using regex";
$b = "I\'m using regex";
$patt = "/(?<!\\\)\'/";
$repl = "\\'";
print "a: ".preg_replace ($patt, $repl, $a)."\n";
print "b: ".preg_replace ($patt, $repl, $b)."\n";
?>
and prints:
a: I\'m using regex
b: I\'m using regex
Remark: matching a backslashe require a triple backslash (\\\).