且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP Reverse Preg_match

更新时间:2022-04-18 15:26:26

如果你肯定想要使用负正则表达式,这可以做到而不是简单地反转正面正则表达式的结果:

This can be done if you definitely want to use a "negative regex" instead of simply inverting the result of the positive regex:

if(preg_match("/^(?:(?!" . $filter . ").)*$/i", $node)) {
    echo $node;
}

将匹配一个字符串,如果它不包含 $ filter 中的正则表达式/子字符串。

will match a string if it doesn't contain the regex/substring in $filter.

说明:(以 office 作为示例字符串)

Explanation: (taking office as our example string)

^          # 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