PHP string get substr and Strstr function-from PHP tutorial

Source: Internet
Author: User
Tags php tutorial

PHP string get substr with strstr function PHP string get

Used to get the specified string from a string.

The relevant functions are as follows:

    • SUBSTR (): Get a portion of it from a string
    • Strstr (): Finds the position of the first occurrence of a string in another string and returns all characters from that position to the end of the string
    • SUBCHR (): With Strstr ()
    • STRRCHR (): Finds the last occurrence of a string in another string and returns all characters from that position to the end of the string
SUBSTR ()

The substr () function is used to get a portion of it from a string and returns a string.

Grammar:

String substr (string string, int start [, int length])
The parameters are described as follows:
Parameters Description
String The string to process
Start String start position, starting at 0, negative starts at the specified position at the end of the string
Length Optionally, the string returns the length, by default, until the end of the string, and the negative is returned from the end of the string

Example:

<?phpecho substr (' abcdef ', 1);      Output Bcdefecho substr (' abcdef ', 1, 2);   Output Bcecho substr (' abcdef ', -3, 2);  Output Deecho substr (' abcdef ', 1,-2);  Output bcd?>
Tips

If start is negative and length is less than or equal to start, length is 0.

Strstr ()

Finds the position of the first occurrence of a string in another string and returns all characters from that position to the end of the string, or FALSE if not found.

Grammar:

String Strstr (String string, String needle)
The parameters are described as follows:
Parameters Description
String The string to process
Needle The string to look for, and if it is a number, search for characters that match the ASCII value of the number

Example:

<?php$email = ' [email protected] '; $domain = Strstr ($email, ' @ '); echo $domain;//Output @5idev.com?>
Tips

The function is case-sensitive. For case-insensitive lookups, use STRISTR ().

STRCHR ()

With Strstr ().

STRRCHR ()

Finds the position of the last occurrence of a string in another string and returns all characters from that position to the end of the string, or FALSE if not found.

Grammar:

String STRRCHR (String string, String needle)

The function behaves the same as the Strstr () function, and the parameter meaning can be found in the above strstr () function parameter description.

Example:

<?php$str= "aaa| bbb| CCC "; Echo strrchr ($str," | ");? >

To run an example, output:

| Ccc

Combining the substr () function allows you to intercept everything that follows the last occurrence of a character :

<?php$str= "aaa| bbb| CCC "; Echo substr (STRRCHR ($str," | "), 1);? >

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.