Php query string learning NOTE _ PHP Tutorial

Source: Internet
Author: User
Php searches for string learning notes. This article introduces some methods for searching strings in php, including searching for strings, searching for string occurrences, and searching for string locations, this article describes some methods for searching strings in php, including searching for strings, searching for string occurrences, and searching for string locations, for more information, see references.

You can use strstr () and strrchr () functions to find characters in php strings. The structure of the two functions is as follows:

Strstr ($ haystack, $ needle)
Strrchr ($ haystack, $ needle)

// $ Haystack indicates the parent string, and $ needle indicates the character to be searched
The strstr () function is used to locate the first occurrence of a character and return the ending part from this position to the parent string;

The strrchr () function is used to locate the last occurrence of a character and return the part from this position to the end of the parent string.

Instance:

The code is as follows:
$ A = "p ";
$ B = "echo is a php function, p is an html tag ";
$ Str1 = strstr ($ B, $ );
If ($ str1)
Echo $ str1 ."
";
Else echo "p not found "."
";

$ Str2 = strrchr ($ B, $ );
If ($ str2)
Echo $ str2;
Else echo "p not found ";
?>

The substr_count () function can be used to query the number of php string occurrences. its structure is as follows:

Substr_count ($ haystack, $ needle [, $ offset [, $ length])
// $ Haystack indicates the parent string, and $ needl indicates the character to be searched
// $ Offset indicates the start point of the search, and $ length indicates the length of the search, all of which are optional parameters.
Instance:

The code is as follows:

$ Str = "this is a test ";
Echo substr_count ($ str, 'is ').'
';
Echo substr_count ($ str, 'is ', 3 ).'
';
Echo substr_count ($ str, 'is ', 3, 3 ).'
';
?>

You can use the strrpos () and strpos () functions to search string locations in php.

Find the last location
Use the strrpos () function. The structure is as follows:

Strrpos ($ haystack, $ needle [, $ offset])
// The parameter $ needle can only be one character, not a string. If a string is provided, only the first character in the string is taken. Other characters are invalid;
// Optional parameter $ offset sets the length of the query string.
Instance 1:

The code is as follows:

$ Text = "php ";
$ Str1 = strrpos ($ text, "p ");
If ($ str1)
Echo "find the character 'P '"."
";
Else echo "cannot find the character 'P '"."
";
$ Str2 = strrpos ($ stxt, "q ");
If ($ str2)
Echo "find the character 'Q '"."
";
Else echo "the character 'Q' is not found '"."
";
?>

Locate the first time
Using the strpos () function, the strrpo () function is only one letter different from the function, but the function is quite different. The $ needle parameter of the strpos () function allows the use of a string and searches for the position of this string in $ haystack for the first time, instead of the last time.

Instance:

The code is as follows:

$ Text = "one of the differences between php and asp: php is more stable and secure ";
$ Str1 = strrpos ($ text, "php ");
If ($ str1)
Echo "find the string 'php' at: $ str1 "."
";
Else echo "the string 'php' is not found '"."
";
$ Str2 = strpos ($ stxt, "php ");
If ($ str2)
Echo "find the string 'php '"."
";
Else echo "the string 'php' is not found '"."
";
?>

...

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.