Home >Backend Development >PHP Tutorial >String PHP implementation method to intercept string from right to left/from left to right

String PHP implementation method to intercept string from right to left/from left to right

WBOY
WBOYOriginal
2016-07-29 08:47:241459browse

Syntax:
substr (string to be intercepted, starting position, interception length)
The starting position starts from 0. If you want to intercept from the first character, the starting position parameter is 0.
The last parameter is optional, If only the starting position is provided, then intercept from the starting position to the end
Look at the example of intercepting from left to right:
1. Intercept from the second character to the end

Copy the code The code is as follows:


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


The output result is: bcdef
2. Intercept 3 characters starting from the 2nd character

Copy the code The code is as follows:


$result = substr (“abcdef”, 1,3);
echo($result);


The output result is: bcd
Intercept from right to left:
1. Intercept 1 character from right to left

Copy the code The code is as follows:


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


The output result is: f
2. Take 2 from right to left characters

Copy the code The code is as follows:


$result = substr ("abcdef", -2);
echo($result);


The output result is: ef
3. From the right 3 characters and 1 character to the left

Copy code The code is as follows:


$result = substr (“abcdef”, -3,1);
echo($result);


Output result for: d

The above introduces the implementation method of string PHP intercepting strings from right to left/from left to right, including string content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn