当斜杠在变量中时,preg_match会发出问题

Aut*_*cus 2 php cakephp php-5.3

我有一个动态的产品列表,我正在检查它.问题是,在我的产品名称中,如果有一个/然后我的preg_match给了我preg_match()[function.preg-match]:未知的修饰符错误

这是一个可行的产品名称示例

$productName = "Samuel Cream Leather Sofa, Loveseat, Club Chair, & Ottoman Set";
Run Code Online (Sandbox Code Playgroud)

这不会奏效

$productName = "Samuel Cream Leather Sofa, Loveseat, Club Chair, w/ & Ottoman Set";



foreach ($results as $result) { 
            $decodedProduct = urldecode($product);
            if(preg_match("/$decodedProduct/",$result->nodeValue)){
            echo $decodedProduct."-------------".$result->nodeValue."<br />";
            }
}
Run Code Online (Sandbox Code Playgroud)

ndm*_*ndm 5

将值插入到不用作正则表达式的正则表达式中时,请始终使用preg_quote.

要避免分隔符冲突,请使用第二个参数preg_quote来定义需要引用的分隔符char:

preg_match("/" . preg_quote($decodedProduct, "/") . "/", $result->nodeValue)
Run Code Online (Sandbox Code Playgroud)