How can I escape 开发者_Python百科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:
. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
It does seem that you want preg_quote
, but perhaps you should detail your situation more, because it’s quite possible that what you are trying to do could be done a better way.
精彩评论