How can I escape incoming data so I can use it as a pattern in preg_replace()
and consorts? For example, I need to match against this string:
/vorschau/
Obviously, I need to escape the "v" or I will get an error.
I can't find anything in the documentation. Is there some sort of addslashes()
for this, or a workaround within the expression?
If I understand your question correctly, you are looking for preg_quote
:
string preg_quote ( string $str [, string $delimiter = NULL ] )
preg_quote()
takesstr
and puts a backslash in front of every character that is part of the regular expression syntax.
This is useful if you have a run-time string that you need to match in some text and the string may contain special regex characters.The special regular expression characters are:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
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