Php string search strstrpreg_match_PHP tutorial

Source: Internet
Author: User
Search strstrpreg_match for the php string. Php string search and matching there are many functions in PHP for searching, matching, or locating. they all have different meanings. Here we will only describe strstr and stristr which are used more frequently. the latter and the former are used to search for and match php strings. many functions are used to search, match, or locate PHP strings. they all have different meanings. Here we will only describe the functions of strstr, stristr. the latter and the former which have the same return values but are case insensitive.

Php Tutorial string search and matching

Php has many functions for searching, matching, or locating. they all have different meanings. Here we will only describe the functions of strstr, stristr. the latter and the former which have the same return values but are case insensitive.

Strstr ("Parent string", "sub-string") is used to locate the first occurrence of a sub-string in the parent string, and return the portion of the parent string from the start of the sub-string to the end of the parent string. For example

Echo strstr ("abcdefg", "e"); // output "efg"

If no substring is found, null is returned. It can be used to determine whether a string contains another string:

$ Needle = "iwind ";
$ Str = "I love iwind ";
If (strstr ($ str, $ needle ))
{
Echo "contains iwind ";
}
Else
{
Echo "there is no iwind ";
}
The output "contains iwind"

Preg_match regular

In the regular expression syntax compatible with preg_match, B represents the word boundary.
So: which of the following is OK ???

$ A = "test, admin, abc ";

$ B = "te ";

$ Exist = preg_match ("/B {$ B} B/", $ );

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 pattern matches. Either 0 (no match) or 1 time, because preg_match () will stop searching after the first match. Preg_match_all () indicates that, on the contrary, the end of the subject is always searched. If an error occurs in preg_match (), false is returned.
Example:

$ A = "abcdefgabcdefaaag ";

Preg_match ('| abc ([a-z] +) g | isu', $ a, $ out1 );

Preg_match_all ('| abc ([s] +) g | isu', $ a, $ out2 );

Echo"

";

Print_r ($ out1 );

Print_r ($ out2 );

Echo"

";

?>

Statement: when double quotation marks are used, they are different from single quotation marks.

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

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

?>


Many functions are used to search for, match, or locate in PHP, which have different meanings. Here we will only describe strstr, stristr. the latter and the former...

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.