php from right to left and intercepts strings from left to right

  • 2020-05-10 17:48:27
  • OfStack

Grammar:
substr(string to intercept, start position, intercept length)

The start position starts at 0, and if you want to intercept from the first character, the start position parameter is 0.
The last parameter is optional, and if only the start position is provided, it is intercepted from the start position to the end

Let's look at an example from left to right:

1. Intercept from the second character to the end
 
$result = substr ( " abcdef " , 1); 
echo($result); 

The output is: bcdef
2. Intercept 3 characters from the second character
 
$result = substr ( " abcdef " , 1,3); 
echo($result); 

The output is: bcd
From right to left:
1. Intercept 1 character from right to left
 
$result = substr ( " abcdef " , -1); 
echo($result); 

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

The output is: ef
3. Intercept 1 character to the left from the third character on the right
 
$result = substr ( " abcdef " , -3,1); 
echo($result); 

The output is: d

Related articles: