PHP String Lookup strstr preg_match_php Tutorial

Source: Internet
Author: User
Tags string find
PHP string lookup and matching in PHP is used to find or match or locate a lot of functions, they all have different meanings. This is just about using more strstr,stristr. The latter and the former function, the return values are the same, but not case-sensitive.

PHP Tutorial string Find and Match

There are very many functions in PHP that are used to find or match or locate, and they all have different meanings. This is just about using more strstr,stristr. The latter and the former function, the return values are the same, but not case-sensitive.

Strstr ("Female string", "substring") is used to find the position where the substring first appears in the parent string, and returns the portion of the parent string from the beginning of the substring to the end of the parent string. Like what

Echo strstr ("ABCDEFG", "E"); Will output "EFG"

If no substring is found, NULL is returned. Because it can be used to determine whether a string contains another string:

$needle = "Iwind";
$str = "I love Iwind";
if (Strstr ($str, $needle))
{
echo "Inside is Iwind";
}
Else
{
echo "There is no iwind";
}
Will output "There's iwind inside."

Preg_match Regular

Preg_match compatible Regular expression syntax B stands for Word boundaries
So: The following should be possible???

$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 relevant instructions

int Preg_match (string pattern, string subject [, array matches [, int flags]]);
Preg_match () returns the number of times that pattern matches. Either 0 times (no match) or 1 times, because Preg_match () stops searching after the first match. Preg_match_all () Instead, it searches until the end of subject. If error Preg_match () returns false.
Example:

$a = "Abcdefgabcdefaaag";

Preg_match (' |abc ([a-z]+) G|isu ', $a, $out 1);

Preg_match_all (' |abc ([s]+) G|isu ', $a, $out 2);

echo "

";

Print_r ($out 1);

Print_r ($out 2);

echo "

";

?>

notation: When using double quotation marks and single quotation marks are different

Preg_match_all ("/href=" (. *) "/isu", $contents, $out);

Preg_match_all (' |href= ' (. *) "|isu", $contents, $out);

?>


http://www.bkjia.com/PHPjc/445410.html www.bkjia.com true http://www.bkjia.com/PHPjc/445410.html techarticle PHP string lookup and matching in PHP is used to find or match or locate a lot of functions, they all have different meanings. This is just about the more strstr,stristr. The latter and the former ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.