尝试将一组单词与用户键入的文本进行匹配时会显示此错误。
foreach($items as $k=>$item){
if($item!='' && preg_match("/".$item."/", $opText)){
if(!in_array($item,$params[$val['name']],true)){
$params[$val['name']][]=$item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
$item
里面有一个/
,这意味着它认为正则表达式比它更早结束。
/goldfish/herring/
// ^ Not actually a modifier (h) - just an unescaped string
Run Code Online (Sandbox Code Playgroud)
您可以使用该preg_quote($string, '/')
功能将其转换为以下内容:
/goldfish\/herring/
// ^ Escaped
Run Code Online (Sandbox Code Playgroud)
用法:
foreach($items as $k=>$item){
if($item!='' && preg_match("/".preg_quote($item,"/")."/", $opText)){
if(!in_array($item,$params[$val['name']],true)){
$params[$val['name']][]=$item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
提示:
如果这是一个硬编码的正则表达式,您可以更改转义字符,使正则表达式更易于阅读,而不必转义常用字符:
~goldfish/herring~
// ^ ^
// | \- Using a different modifier
// |
// \- Different from the modifier, so we don't have to escape it
Run Code Online (Sandbox Code Playgroud)