We all know that in JS string intercept characters have functions substr and substring, that PHP, PHP is not directly available substring functions, but there are substr functions.
Do not believe that you can test. The following is the correct code.
?
$a = "Me";
Echo (substr ($a,,))//Output me
?>
the following error code.
?
$a = "Me";
Echo (subString ($a,,));
? >
The substr () function returns part of the string.
SUBSTR (String,start,length)
String: Strings to intercept
Start
Positive number-starts at the specified position of the string
Negative number-starts at the specified position at the end of the string
0-Starts at the first character in the string
Length
Optional. Specify the length of the string to return. The default is until the end of the string.
Positive number-returns from the location where the start parameter is located
Negative-returns from the end of the string
The usage of PHP substr () is detailed
Definitions and usage
The substr () function returns part of the string. Using the substr () function to intercept Chinese may appear garbled, it is recommended to use the MB_SUBSTR () function to intercept Chinese.
Grammar
SUBSTR (String,start,length)
parameters |
description |
string |
necessary. A string that stipulates the part to return. |
Start |
Necessary. Specify where to start the string.
- Positive-starts at the specified position of the string
- Negative-starts at the specified location from the end of the string
- 0-starts at the first character in the string
|
length |
Optional. Specify the length of the string to return. The default is until the end of the string.
- Positive number-returns from the location where the start parameter is located
- Negative-returns from the end of the string
|
Tips and comments
Note: If start is a negative number and length is less than or equal to start, length is 0.
Example
<?php
$str = ' Hello world! ';
Echo substr ($STR, 4); o world! Start at the beginning of the 4th right to the end
of Echo substr ($str, 4, 5);//O wor from 4th to right 5
substr ($STR, 4,-1);//O World left 4th and right 1th between characters
echo substr ($STR,-8, 4); O wo Right start 8th intercept 4-bit
echo substr ($str, -8,-2);//O worl right up 8th and right up 2nd characters
?>