Talking 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 in JS string intercept characters have functions substr and substring, that PHP, PHP is not directly available substring functions, but there are substr functions.
Do not believe that you can test. The following is the correct code.

?
$a = "Me";
Echo (substr ($a,,))//Output me
?>
the following error code.
?
$a = "Me";
Echo (subString ($a,,));
? >

The substr () function returns part of the string.

SUBSTR (String,start,length)

String: Strings to intercept

Start

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

Length

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

The usage of PHP substr () is detailed

Definitions and usage

The substr () function returns part of the string. Using the substr () function to intercept Chinese may appear garbled, it is recommended to use the MB_SUBSTR () function to intercept Chinese.

Grammar

SUBSTR (String,start,length)

parameters description
string necessary. A string that stipulates the part to return.
Start

Necessary. Specify where to start the string.

    • Positive-starts at the specified position of the string
    • Negative-starts at the specified location from the end of the string
    • 0-starts at the first character in the string
length

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

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

Tips and comments

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

Example

<?php
$str = ' Hello world! ';
Echo substr ($STR, 4); o world! Start at the beginning of the 4th right to the end
of Echo substr ($str, 4, 5);//O wor from 4th to right 5
substr ($STR, 4,-1);//O World left 4th and right 1th between characters 
   echo substr ($STR,-8, 4); O wo Right start 8th intercept 4-bit
echo substr ($str, -8,-2);//O worl right up 8th and right up 2nd characters
?>

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.