redmonkey wrote:In many cases when using regex a double backslash is all that is required, however PHP seems to handle things slightly different.
As many other languages do
redmonkey wrote:
For anyone who happens to be interested, the reason you require four backslashes when trying to pass a literal backslash character is that PHP seems to parse the regex pattern first prior to envoking the regex engine.
It does so because regexp pattern
is a php string (unlike Javascript, where regexes are one of internal datatypes). PHP uses backslash for its own purposes, e.g. escaping special characters in strings.
redmonkey wrote:
Therefore the pattern '\\\'' is actually passed to the regex engine as '\''.
Carrying on from Weirdan's example above, what makes life even more confusing is that....
Code: Select all
echo (preg_match('/\\\/', '\'')?"true":"false");
...also results in true even though we are only using three backslashes in the pattern.
Excellent example, I happenned to check it too.

It's unusual behaviour among the programming languages I ever used, PHP treats backslash before non-special character not as escape character, but as backslash itself!

Ugly, but it works so. It's documented feature:
http://php.net/manual/en/language.types ... tax.double
Let's have a look at pattern you used:
- First character is a forward slash. It's non-special character, so it parsed as is
- Second character is a backslash, followed by special character (backslash again
). So entire 2char sequence parsed as literal last-in-sequence character, giving us a literal backslash.
- (Here lies ugly dragon
) Forth character is a backslash, followed by non-special character!. Interpeted as a literal backslash
- Fifth character is a forward slash. Non-special character, interpreted as is
We've just seen Black PHP Magic in action

So, the pattern passed to preg_match is: /\\/
Obviously, this pattern matches literal backslash.
redmonkey wrote:
Even though the example I give above is functionally identical to....
Code: Select all
echo (preg_match('/\\\\/', '\'')?"true":"false");
....I would recommend always using four backslashes when passing the literal backslash character as I think it is more technically correct.
It isn't more correct in php world, but I consider it's good habit. Someday you could move to another language.......