Talk about the correct usage of substr and substring in PHP and the introduction of related parameters _php example

Source: Internet
Author: User
We all know that the string intercept characters in JS have functions substr and substring, and PHP, PHP, there is no directly available substring function, but there are substr functions.
Don't believe you can test it. Here's a good piece of code.

< $a = "Me"; Echo (substr ($a,,));//Output me?> The following is an error code <? $a = "Me"; Echo (subString ($a,,)); >

The substr () function returns a portion of a string.

SUBSTR (String,start,length)

String: Strings to intercept

Start

Positive number-starts at the specified position in the string
Negative number-starts at the specified position from the end of the string
0-Starts at the first character in a string

Length

Optional. Specifies the length of the string to return. The default is until the end of the string.
Positive number-returns from the position where the start parameter is located
Negative-returns from the end of the string

The usage of PHP substr () is detailed

Definition and usage

The substr () function returns a portion of a string. Using the substr () function to intercept Chinese can be garbled and it is recommended to use the MB_SUBSTR () function to intercept Chinese.

Grammar

SUBSTR (String,start,length)

Parameters Description
String Necessary. A string that specifies the part to return.
Start

Necessary. Specifies where to start the string.

    • Positive-starts
    • negative-start
    • 0-starts at the first character in the string
Length

Optional. Specifies the length of the string to return. The default is until the end of the string.

    • Positive number-returns from the position where the start parameter is located
    • Negative-returns from the end of the string

Hints and Notes

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

Example

<?php$str = ' Hello world! '; Echo substr ($STR, 4); o world! Start from the 4th to the right to the end of Echo substr ($str, 4, 5); O Wor 5-bit echo substr ($STR, 4,-1) to the right from the left 4th; O World left 4th and right from the 1th character Echo substr ($str,-8, 4); O wo right from the 8th start to intercept the right 4-bit echo substr ($STR, -8,-2); o Worl the 8th and right from the 2nd character?>
  • 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.