Php string search and matching there are many functions in PHP for searching, matching, or locating. They all have different meanings. Here we will only describe the functions of strstr, stristr. The latter and the former which have the same return values but are case insensitive.
Php tutorial string search and matching
Php has many functions for searching, matching, or locating. They all have different meanings. Here we will only describe the functions of strstr, stristr. The latter and the former which have the same return values but are case insensitive.
Strstr ("parent string", "sub-string") is used to locate the first occurrence of a sub-string in the parent string, and return the portion of the parent string from the start of the sub-string to the end of the parent string. For example
Echo strstr ("abcdefg", "e"); // output "efg"
If no substring is found, null is returned. It can be used to determine whether a string contains another string:
$ Needle = "iwind ";
$ Str = "I love iwind ";
If (strstr ($ str, $ needle ))
{
Echo "contains iwind ";
}
Else
{
Echo "there is no iwind ";
}
The output "contains iwind"
Preg_match regular
In the regular expression syntax compatible with preg_match, B Represents the word boundary.
So: Which of the following is OK ???
$ A = "test, admin, abc ";
$ B = "te ";
$ Exist = preg_match ("/B {$ B} B/", $ );
If ($ exist)
{
Echo "exists ";
} Else
{
Echo "does not exist ";
}
Take a look at the relevant instructions
Int preg_match (string pattern, string subject [, array matches [, int flags]);
Preg_match () returns the number of times pattern matches. Either 0 (no match) or 1 time, because preg_match () will stop searching after the first match. Preg_match_all () indicates that, on the contrary, the end of the subject is always searched. If an error occurs in preg_match (), false is returned.
Example:
$ A = "abcdefgabcdefaaag ";
Preg_match ('| abc ([a-z] +) g | isu', $ a, $ out1 );
Preg_match_all ('| abc ([s] +) g | isu', $ a, $ out2 );
Echo"
";
Print_r ($ out1 );
Print_r ($ out2 );
Echo"
";
?>
Statement: When double quotation marks are used, they are different from single quotation marks.
Preg_match_all ("/href =" (. *) "/isu", $ contents, $ out );
Preg_match_all ('| href = "(. *)" | isu', $ contents, $ out );
?>