PHP Lookup String Common functions Introduction _php tips

Source: Internet
Author: User
Tags mixed
First occurrence of a strstr-lookup string

String Strstr (String $haystack, mixed $needle [, bool $before _needle = false])
Note 1: $haystack is the string of the party, $needle is the string being searched. This function is case-sensitive.
Note 2: The return value starts from the needle to the end.
Note 3: With respect to $needle, if it is not a string, it is used as an integer for the number of characters.
Note 4:before_needle If true, returns the previous object.
Copy Code code as follows:

<?php
$email = ' yuxiaoxiao@example.com ';
$domain = Strstr ($email, ' @ ');
Echo $domain; Print @example. com
$user = Strstr ($email, ' @ ', true); From PHP 5.3.0
Echo $user; Print Yuxiaoxiao
?>


Second, Stristr strstr case-insensitive version
Third, Strpos-find the first occurrence of the string position

int Strpos (string $haystack, mixed $needle [, int $offset = 0])
Note 1: The optional offset parameter can be used to specify which character from the haystack to start the lookup. The number position returned is relative to the starting position of the haystack.
Iv. substr-Returns a substring of a string

String substr (string $string, int $start [, int $length])
$rest = substr ("abcdef",-1); Return "F"

Note 1: If Start is Non-negative, the returned string starts at the start position of string and starts at 0. For example, in the string "ABCdef", the character at position 0 is "a", and the string at position 2 is "C" and so on.

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

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

Length

Note 4: If the length of a positive number is provided, the returned string will begin at the start with a maximum of length characters (depending on the length of the string).

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

Note 6: If you provide length with a value of 0,false or NULL, an empty string is returned.

Note 7: If length is not supplied, the returned substring starts at the start position until the end of the string.
Copy Code code as follows:

<?php
$rest = substr ("abcdef", 0,-1); Return to "ABCDE"
$rest = substr ("ABCdef", 2,-1); Back to "CDE"
$rest = substr ("ABCdef", 4,-4); Returns ""
$rest = substr ("ABCdef",-3,-1); Return to "de"
?>

V. STRRCHR-Find the last occurrence of the specified character in the string

String STRRCHR (String $haystack, mixed $needle)

This function returns a portion of the haystack string that begins at the last occurrence of needle until the end of haystack.

Vi. Strripos-Calculates where the specified string last appears in the target string (case-insensitive)
Vii. Stripos-Find the position of the first occurrence of the string (case-insensitive)
Viii. Strrpos-Computes the last occurrence of the 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.