PHP how to intercept strings from right to left/left to right _php tips

Source: Internet
Author: User
Syntax:
SUBSTR (string to be intercepted, start position, intercept length)

The start position starts at 0 and if you want to intercept from the first character, the start position parameter is 0.
The last parameter is optional, and if only the start position is provided, it is intercepted from the start position to the last

First Look at the example from left to right:

1, from the 2nd character intercept to the last
Copy Code code as follows:

$result = substr ("abcdef", 1);
Echo ($result);

The output is: bcdef
2, starting from the 2nd character Intercept 3
Copy Code code as follows:

$result = substr ("abcdef", 1,3);
Echo ($result);

The output is: BCD
Intercept from right to left:
1. Intercept 1 characters from right to left
Copy Code code as follows:

$result = substr ("abcdef",-1);
Echo ($result);

The output is: F
2. Intercept 2 characters from right to left
Copy Code code as follows:

$result = substr ("ABCdef",-2);
Echo ($result);

The output is: EF
3, from the right 3rd character to intercept the left 1 characters
Copy Code code as follows:

$result = substr ("abcdef", -3,1);
Echo ($result);

The output is: D

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.