Possible Duplicate:
Is there a PHP function that can escape regex patterns before they are applied?
I want to use a string stored in a variable in a regular expression. What's the best way (in PHP) to escape a string for use in a (PCRE) regular expression?
For example,
$text = 'm/z'; // this is the text I want to use as part of my regular expression
$text_regexp = '#' . $text . '#i'; // this is my regular expression
Would give
$text_regexp = '#m/z#i';
But I would want the following regular expression:
$text_regexp = '#m\/z#i';
This is a contrived example, but I wanted to illustrate the point simply.