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

Implementation method of intercepting strings from right to left/left to right in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:22:24937browse

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, then the starting position The 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. Cut from the second character to the last

Copy the code The code is as follows:

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

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

$result = substr (“abcdef”, 1,3);
echo($result ; Copy the code
The code is as follows:


$result = substr ("abcdef", -1);
echo($result);
The output result is: f 2. Cut 2 characters from right to left

Copy the code

The code is as follows:


$result = substr (“abcdef”, -2);
echo($result);
The output result is: ef 3. From the 3rd character on the right to the left Intercept 1 character

Copy code

The code is as follows:


$result = substr ("abcdef", -3,1) ;
echo($result);
The output result is: d
http://www.bkjia.com/PHPjc/324732.html

www.bkjia.com
true
http: //www.bkjia.com/PHPjc/324732.html

TechArticle Syntax: substr (string to be intercepted, starting position, intercepting length) The starting position starts from 0, if you want Starting from the first character, the starting position parameter is 0. The last parameter is...
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