Home > Article > Backend Development > PHP regular does not contain a certain string sample code
This article introduces how to write PHP regular expressions that do not contain a certain string. Examples of regular expressions that do not contain a specified string. Friends in need can refer to it.
In PHP programming, the common function for matching strings is strstr($str, “abc”); Regular matching preg_match(”/(abc)?/is”, $str);But if you want to match a string that does not contain a certain string, it is more troublesome to use regular expressions. The problem can be solved if the regular expression !strstr($str, “abc”); is not used. Use regular names like this: "/^((?!abc).)*$/is"Example: The result is: false, containing abc! $str = "2b3c4d5c";Note: [^(abc)] This syntax checks whether the characters in $str are not in a b c one by one. preg_match(”/[^(abc)]/s”, $str, $arr);The character 2 is not in a b c, so the return value of $arr is 2; Matches both the string "abc" and not the string "xyz" "/(abc)[^((?!xyz).)*$]/s" |