note 36678 added to function.preg-replace

From: Date: Sat, 18 Oct 2003 07:37:13 +0000
Subject: note 36678 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-58800@lists.php.net to get a copy of this message
I spent some time fighting with this, so hopefully this will help someone else. Escaping a backslash (\) really involves not two, not three, but four backslashes to work properly. So to match a single backslash, one should use: preg_replace('/(\\\\)/', ...); or to, say, escape single quotes not already escaped, one could write: preg_replace("/([^\\\\])'/", "\$1\'", ...); Anything else, such as the seemingly correct preg_replace("/([^\\])'/", "\$1\'", ...); gets evaluated as escaping the ] and resulting in an unterminated character class. I'm not exactly clear on this issue of backslash proliferation, but it seems to involve the combination of PHP string processing and PCRE processing. ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36678 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36678&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36678&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#58800) next »