I am trying to match 008/
preg_match('/008\\//i', '008/', $matches);
preg_match('/008\//i', '008/', $matches);
My question is why do both of the regular expressions work. I would expect the second to work, but why does the double backslash one work?
The preg_match() function returns whether a match was found in a string.
preg_match is case sensitive. A match. Add the letter "i" to the end of the pattern string to perform a case-insensitive match.
Because \\
in PHP strings means "escape the backslash". Since \/
doesn't mean anything it doesn't need to be escaped (even though it's possible), so they evaluate to the same.
In other words, both of these will print the same thing:
echo '/008\\//i'; // prints /008\//i
echo '/008\//i'; // prints /008\//i
The backslash is one of the few characters that can get escaped in a single quoted string (aside from the obvious \'
), which ensures that you can make a string such as 'test\\'
without escaping last quote.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With