Skip to content
Advertisement

preg_quote() bug or strange behavior?

I used preg_quote() to escape some special characters. But in the test below, there is something I can’t understand.

Why ‘date:1111aaa’ doesn’t match ???

JavaScript

http://sandbox.onlinephpfunctions.com/code/9ef36e2b010ac1f4f0eaf08881004892bd0cd9c4

Advertisement

Answer

You’re using preg_quote in the wrong place, it is intended for escaping characters in the pattern, not the test string (see the manual). Change your code to this and it works fine:

JavaScript

Output:

JavaScript

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement