note 59533 added to function.preg-replace

From: Date: Fri, 09 Dec 2005 08:16:48 +0000
Subject: note 59533 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-99800@lists.php.net to get a copy of this message
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 (\\\). ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59533 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59533&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59533&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99800) next »