【发布时间】:2011-06-23 14:35:52
【问题描述】:
可能重复:
Is there a PHP function that can escape regex patterns before they are applied?
我想在正则表达式中使用存储在变量中的字符串。 (在 PHP 中)转义字符串以用于(PCRE)正则表达式的最佳方法是什么?
例如,
$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
愿意
$text_regexp = '#m/z#i';
但我想要以下正则表达式:
$text_regexp = '#m\/z#i';
这是一个人为的例子,但我想简单地说明这一点。
【问题讨论】: