php string lookup strstr preg_match

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags different echo find finding function functions network network programming

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

?>

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.