Common functions for searching strings in PHP

Source: Internet
Author: User
Common functions for searching strings in PHP. This section describes the use of strstr. 1. the first appearance of strstr.

String strstr (string $ haystack, mixed $ needle [, bool $ before_needle = false])
Note 1: $ haystack is the string and $ needle is the string to be searched. This function is case sensitive.
Note 2: The return value starts from needle to the end.
Note 3: For $ needle, if it is not a string, it is used as an integer as the character serial number.
Note 4: If before_needle is true, the first thing is returned.
The code is as follows:
$ Email = 'yuxiaoxiao @ example.com ';
$ Domain = strstr ($ email ,'@');
Echo $ domain; // Print @ example.com
$ User = strstr ($ email, '@', true); // starting from PHP 5.3.0
Echo $ user; // Print yuxiaoxiao
?>


II. stristr strstr case-insensitive versions
III. strpos-find the position where the string first appears

Int strpos (string $ haystack, mixed $ needle [, int $ offset = 0])
Note 1: The optional offset parameter can be used to specify which character in haystack to start searching. The returned numeric position is relative to the starting position of the haystack.
4. substr-return the substring of the string

String substr (string $ string, int $ start [, int $ length])
$ Rest = substr ("abcdef",-1); // return "f"

Note 1: If start is not negative, the returned string starts from the start position of string and starts from 0. For example, in the string "abcdef", the character at the position 0 is "a", and the character string at the position 2 is "c.

Note 2: If start is a negative number, the returned string starts from the start character at the end of the string.

Note 3: If the string length is less than or equal to start, FALSE is returned.

Length

Note 4: If a positive length is provided, the returned string will contain a maximum of length characters starting from start (depending on the length of the string ).

Note 5: If a negative length is provided, many characters at the end of the string will be missed (if start is a negative number, it will be counted from the end of the string ). If start is not in this text, an empty string is returned.

Note 6: If the length value is 0, FALSE, or NULL, an empty string is returned.

Note 7: If length is not provided, the returned substring starts from the start position until the end of the string.
The code is as follows:
$ Rest = substr ("abcdef", 0,-1); // return "abcde"
$ Rest = substr ("abcdef", 2,-1); // return "cde"
$ Rest = substr ("abcdef", 4,-4); // return ""
$ Rest = substr ("abcdef",-3,-1); // return "de"
?>

5. strrchr-find the last occurrence of a specified character in the string

String strrchr (string $ haystack, mixed $ needle)

This function returns a part of the haystack string, starting with the last position of the needle until the end of the haystack.

6. strripos-calculate the position of the last occurrence of the specified string in the target string (case insensitive)
VII. stripos-locate the first occurrence of a string (no size difference)
VIII. strrpos-calculate the position of the last occurrence of a specified string in the target string

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.