php tutorial string search and match
There are many functions in php for finding or matching or locating, and they all have different meanings. Here only about the more strstr, stristr. The latter and the former function, the return value is the same, but not case-sensitive.
strstr ("parent string", "substring") is used to find the first occurrence of a substring in the parent string and returns the part of the parent string that begins with the substring and ends with the parent string . such as
echo strstr ("abcdefg", "e"); // will print "efg"
If no substring is found, it returns null. Because it can be used to determine whether a string contains another string:
$ needle = "iwind";
$ str = "i love iwind";
if (strstr ($ str, $ needle))
{
echo "iwind" inside;
}
else
{
echo "there is no iwind";
}
Will be output "inside iwind" preg_match regular
Preg_match compatible regular expression syntax b on behalf of the word boundary So: the following should be? ? ?
$ a = "test, admin, abc";
$ b = "te";
$ exist = preg_match ("/ b {$ b} b /", $ a);
if ($ exist)
{
echo "exists";
} else
{
echo "does not exist";
}
Take a look at the instructions
int preg_match (string pattern, string subject [, array matches [, int flags]]);
preg_match () Returns the number of pattern matches. Either zero (no match) or one, because preg_match () will stop searching after the first match. In contrast, preg_match_all () will always search for the end of the subject. If an error preg_match () returns false.
Example:
<? php
$ a = "abcdefgabcdefaaag";
preg_match ('| abc ([az] +) g | isu', $ a, $ out1);
preg_match_all ('| abc ([s] +) g | isu', $ a, $ out2);
echo "<pre>";
print_r ($ out1);
print_r ($ out2);
echo "</ pre>";
?>
Wording:
Use double quotes and single quotes
<? php
preg_match_all ("/ href =" (. *) "/ isu", $ contents, $ out);
preg_match_all ('| href = "(. *)" | isu', $ contents, $ out);
?>