Home > Backend Development > PHP Tutorial > 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

高洛峰
Release: 2023-03-04 15:10:01
Original
2693 people have browsed it

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);
Copy after login

The output result is: bcdef
2. From the 2nd character, 3 characters are intercepted

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

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);
Copy after login

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

$result = substr (“abcdef”, -2); 
echo($result);
Copy after login

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);
Copy after login

Please pay attention to more related articles on how to intercept strings from right to left/left to right in PHP PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template