开发者

addslashes() for preg_replace() and co.?

开发者 https://www.devze.com 2022-12-14 02:05 出处:网络
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:

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() takes str 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消