Addslashes () for preg_replace () and co.?

How can I avoid incoming data, so I can use it as a template in preg_replace() and spouses? For example, I need to match this line:

 /vorschau/ 

Obviously, I need to escape the "v" or I will get an error.

I can not find anything in the documentation. For this, is there some kind of addslashes() or a workaround inside the expression?

+4
source share
2 answers

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 places a backslash in front of each character, which is part of the regular expression syntax.
This is useful if you have a runtime string that you need to match in some text, and the string may contain special regular expression characters.

Special regex characters:. . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

+19
source

It seems that you want preg_quote , but maybe you should describe your situation in more detail, because it is quite possible that what you are trying to do can be done better.

0
source

All Articles