Common function Strstr ($STR, "abc");
Regular matching Preg_match ("/(ABC)/is", $str);
But to match a string that does not contain a string, it is more troublesome to use the positive
If not regular!strstr ($STR, "abc"); We can solve the problem.
But with the regular, it's the only way, "/^ (?!) ABC).) *$/is "
//------------------------------------------------
Copy Code code as follows:
<?php
$str = "DFADFADF765577ABC55FD";
$pattern _url = "/^ (?! ABC).) *$/is ";
if (Preg_match ($pattern _url, $str)) {
echo "does not contain abc!. ";
}else{
echo "contains abc! ";
}
?>
//------------------------------------------------
The result is: false, containing abc!
$str = "2b3c4d5c";
Note: [^ (ABC)] This syntax is to check whether the characters in $str are not in a B C,
Preg_match ("/[^ (ABC)]/s", $str, $arr);
Where the character 2 is not in a B C, so the $arr return value is 2;
Matches at the same time, contains the string "ABC" and does not contain the string "XYZ"
"/(ABC) [^ (?! XYZ).) *$]/S "
The following are supplementary:
There are many ways to determine whether a string contains another string, as follows:
1. Common functions
Strstr ($STR, "abc");
2. Regular matching
Preg_match ("/(ABC)/is", $str);
But to match a string that doesn't contain a string, it's more troublesome to use the positive.
1. If not, the following can be used to solve the problem
!strstr ($STR, "abc");
2. But with the regular, it's the only way.
Preg_match ("/^" (?!) ABC).) *$/is ", $STR);
Complete code example
Copy Code code as follows:
$str = "DFADFADF765577ABC55FD";
$pattern _url = "/^ (?! ABC).) *$/is ";
if (Preg_match ($pattern _url, $STR))
{
echo "does not contain abc!. ";
}
Else
{
echo "contains abc! ";
}
The result is: false, containing abc!
Matches, contains the string "ABC" and does not contain a regular expression of the string "xyz":
Preg_match ("/(ABC) [^] [(?!] XYZ).) *$]/is ", $STR);
This method is effective, I use the following methods:
(?:(?! <\/div>). | \ n) *? Match a string that does not contain </div>
But the result of the final use is that the method is extremely inefficient, you can consider using a very short text (more than 10 words, or up to dozens of) for the same part of the regular formula to be used, but do not use it for a large section of the article, or if you need to replace it in more than one place, consider replacing it with another method such as: Parsing the text that matches the regular style of the paragraph, and then verifying that there is a paragraph of text in it, the regular expression is not a very efficient way to match text segments that do not contain a specific string.