note 36678 deleted from function.preg-replace by colder

From: Date: Mon, 18 Jun 2007 19:49:42 +0000
Subject: note 36678 deleted from function.preg-replace by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127665@lists.php.net to get a copy of this message
Note Submitter: Travis ---- 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.

« previous php.notes (#127665) next »