How to Use php to extract strings from right to left/from left to right

Source: Internet
Author: User
The asp and asp functions that intercept strings are simple and easy to understand: left and right. in php, intercept string from left to right and intercept string from right to left is a function: substr.

The asp and asp functions that intercept strings are simple and easy to understand: left and right. in php, intercept string from left to right and intercept string from right to left is a function: substr.

Syntax:
Substr (string to be intercepted, start position, and truncation length)

The start position starts from 0. If you want to extract from the first character, the start position parameter is 0.
The last parameter is optional. If only the start position is provided, it is truncated from the start position to the end.

Let's take a look at the example from left to right:

1. Capture from 2nd characters to the end
The Code is as follows:
$ Result = substr ("abcdef", 1 );
Echo ($ result );

The output result is bcdef.
2. extract 3 from 2nd characters
The Code is as follows:
$ Result = substr ("abcdef", 1, 3 );
Echo ($ result );

The output result is bcd.
Truncate from right to left:
1. extract 1 character from right to left
The Code is as follows:
$ Result = substr ("abcdef",-1 );
Echo ($ result );

Output result: f
2. Extract 2 Characters from right to left
The Code is as follows:
$ Result = substr ("abcdef",-2 );
Echo ($ result );

Output result: ef
3. extract 1 character from the right side of 3rd characters to the left
The Code is as follows:
$ Result = substr ("abcdef",-3, 1 );
Echo ($ result );

Output result: 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.