Home  >  Article  >  Backend Development  >  Implementation method of intercepting strings from right to left/left to right in php

Implementation method of intercepting strings from right to left/left to right in php

高洛峰
高洛峰Original
2017-01-03 15:14:582599browse

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!

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