PHP string search strstr preg_match_PHP tutorial

WBOY
Release: 2016-07-20 11:01:43
Original
1740 people have browsed it

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);

?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445410.htmlTechArticlephp string search and matching There are many functions in PHP for finding or matching or positioning, and they are all different meaning. Here we only describe the functions of the more commonly used strstr and strstr. The latter and the former...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template