Example of substr () function parameter description and usage in php

Source: Internet
Author: User
This article mainly introduces the description and usage of substr () function parameters in php, analyzes the meaning of each parameter in substr () function in the form of an instance, and illustrates its usage, for more information about substr () function parameters in php, see the following example. Share it with you for your reference. The details are as follows:

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 right to left.

String is required, which specifies that a part of the string is to be returned.

Start is required, specifying where the string starts.

Charlist (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

The PHP instance code is as follows:

The code is as follows:

$ Rest_1 = substr ("abcdef", 2); // returns "cdef"
$ Rest_2 = 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"

I hope this article will help you with PHP programming.

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.