PHP find string Common functions Introduction

Source: Internet
Author: User
The first occurrence of a strstr-lookup string

String Strstr (String $haystack, mixed $needle [, bool $before _needle = false])
Note 1: $haystack is a string of characters, $needle is the string being looked up. The 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 the ordinal of the character as an integer.
Note 4:before_needle If true, the previous thing is returned.

<?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?>

Two, stristr strstr case-insensitive version
Three, Strpos-find the first occurrence of the string

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

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

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

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

Note 3: Returns FALSE if the length of the string is less than or equal to start.

Length

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

Note 5: If the length of a negative number is provided, then many of the characters at the end of the string will be omitted (if start is negative, count 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 provided, the returned substring starts at the start position until the end of the string.

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

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

String STRRCHR (String $haystack, mixed $needle)

The function returns a portion of the haystack string, starting at the last occurrence of needle, until the end of haystack.

Vi. Strripos-Calculates the position of the last occurrence of the specified string in the target string (case insensitive)
Vii. Stripos-Find where the string first appears (not case-sensitive)
Viii. Strrpos-Calculates the position of the last occurrence of the specified string in the target string

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.