Home >Backend Development >PHP Tutorial >Implementation method of intercepting strings from right to left/left to right in php
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, it will be intercepted from the starting position to the end.
First look at the example of intercepting from left to right:
1. From The 2nd character is intercepted to the end
$result = substr (“abcdef”, 1); echo($result);
The output result is: bcdef
2. From the 2nd character, 3 characters are intercepted
$result = substr (“abcdef”, 1,3); echo($result);
The output result is: bcd
From the right Intercept to the left:
1. Intercept 1 character from right to left
$result = substr (“abcdef”, -1); echo($result);
The output result is: f
2. Intercept 2 characters from right to left
$result = substr (“abcdef”, -2); echo($result);
Output The result is: ef
3, intercept 1 character from the 3rd character on the right to the left
$result = substr (“abcdef”, -3,1); echo($result);
Please pay attention to more related articles on how to intercept strings from right to left/left to right in PHP PHP Chinese website!