<分区>
if(preg_match("/" . $filter . "/i", $node)) {
echo $node;
}
这段代码过滤一个变量来决定是否显示它。 $filter 的一个示例条目是“office”或“164(.*)976”。
想知道有没有简单的说法:如果$filter在$node中不匹配。以正则表达式的形式?
所以...不是“if(!preg_match”,而是 $filter = “!office” 或 “!164(.*)976”,但它有效吗?
<分区>
if(preg_match("/" . $filter . "/i", $node)) {
echo $node;
}
这段代码过滤一个变量来决定是否显示它。 $filter 的一个示例条目是“office”或“164(.*)976”。
想知道有没有简单的说法:如果$filter在$node中不匹配。以正则表达式的形式?
所以...不是“if(!preg_match”,而是 $filter = “!office” 或 “!164(.*)976”,但它有效吗?
最佳答案
如果您确定要使用“负正则表达式”而不是简单地反转正则表达式的结果,则可以这样做:
if(preg_match("/^(?:(?!" . $filter . ").)*$/i", $node)) {
echo $node;
}
如果字符串不包含 $filter
中的正则表达式/子字符串,将匹配该字符串。
说明:(以office
为例)
^ # Anchor the match at the start of the string
(?: # Try to match the following:
(?! # (unless it's possible to match
office # the text "office" at this point)
) # (end of negative lookahead),
. # Any character
)* # zero or more times
$ # until the end of the string
关于PHP 反向 Preg_match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704061/