Php substr () function parameter description and example tutorial

Source: Internet
Author: User

Php tutorial substr () function parameter description and example tutorial
/*
String substr (string $ string, int $ start [, int $ length]), which can be used to search for matched strings or characters in a long string. $ String is the string to be processed, $ start is the start position, and $ length is the length to be selected.

$ Length: reads characters from left to right for positive data,
When $ length is negative, it reads characters from the right to the left.


*/

$ Rest1 = substr ("abcdef", 2); // returns "cdef"
$ Rest2 = substr ("abcdef",-2); // returns "ef"

$ Rest1 = substr ("abcdef", 0, 0); // returns ""
$ Rest2 = substr ("abcdef", 0, 2); // returns "AB"
$ Rest3 = substr ("abcdef", 0,-1); // returns "abcde"
$ Rest4 = substr ("abcdef", 2, 0); // returns ""
$ Rest5 = substr ("abcdef", 2, 2); // returns "cd"
$ Rest6 = substr ("abcdef", 2,-1); // returns "cde"
$ Rest7 = substr ("abcdef",-2, 0); // returns ""
$ Rest8 = substr ("abcdef",-2, 2); // returns "ef"
$ Rest9 = substr ("abcdef",-2,-1); // returns "e"

 
/*

String is required. Specifies that a part of the string is to be returned.
Start is required. Specifies where the string starts.

Charlist is optional. Specifies the length of the string to be returned. The default value is until the end of the string.
Positive number-start at the specified position of the string
Negative number-starting from the specified position at the end of the string
0-Start at the first character in the 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.