PHP string search and matching There are many functions for searching, matching or positioning in PHP, and they all have different meanings. Here we only describe the more commonly used strstr and stristr. The functions of the latter and the former are the same, but the return values are the same, but they are not case-sensitive.
php tutorial string search and matching
There are many functions in PHP for searching, matching or positioning, and they all have different meanings. Here we only talk about the more commonly used strstr and stristr. The functions of the latter and the former are the same, but the return values are the same, but they are not case-sensitive.
strstr("Mother string", "Substring") is used to find the position of the first occurrence of the substring in the mother string, and returns the position from the beginning of the substring to the mother string in the mother string Ending part. For example
echo strstr("abcdefg", "e"); //will output "efg"
Returns empty if the substring is not found. Because it can be used to determine whether a string contains another string:
$needle = "iwind";
$str = "i love iwind";
if (strstr($str, $needle))
{
echo "There is iwind inside";
}
else
{
echo "There is no iwind in it";
}
Will output "iwind inside"
preg_match regular
In preg_match compatible regular expression syntax, b represents word boundary
So: The following should be possible? ? ?
$a="test,admin,abc";
$b="te";
$exist=preg_match("/b{$b}b/",$a);
if($exist)
{
echo "existence";
}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 was matched. Either 0 times (no match) or 1 time, since preg_match() will stop searching after the first match. preg_match_all(), on the contrary, will search until the end of subject. preg_match() returns false on error.
Example:
$a = "abcdefgabcdefaaag";
preg_match('|abc([a-z]+)g|isu',$a,$out1);
preg_match_all('|abc([s]+)g|isu',$a,$out2);
echo "
";Copy after login
print_r($out1);
print_r($out2);
echo "
";
?>
Writing: The difference between using double quotes and single quotes
preg_match_all("/href="(.*)"/isu",$contents,$out);
preg_match_all('|href="(.*)"|isu',$contents,$out);
?>