Php obtains the first few characters of the string (substr returns the substring usage of the string), and the string substr
In actual project applications, php is often used to obtain the first few digits of a string for comparison and assignment. Today, we will share with you how to use php substr to obtain the first, last, and positioning digits of a string.
Substr
(PHP 4, PHP 5)
Substr-returns the substring of the string.
Description
String substr (string $ string, int $ start [, int $ length])
Returns the substring specified by the start and length parameters.
Parameters
String
Input string.
Start
If start is not negative, the returned string starts from the start position of the string and starts from 0. For example, in the string "abcdef", the character at the position 0 is "a", and the character string at the position 2 is "c.
If start is a negative number, the returned string starts from the start character at the end of the string.
If the string length is less than or equal to start, FALSE is returned.
Example #1 use negative start
<? Php $ rest = substr ("abcdef",-1); // return "f" $ rest = substr ("abcdef",-2 ); // return "ef" $ rest = substr ("abcdef",-3, 1); // return "d"?>
Length
If a positive length is provided, the returned string can contain a maximum of length characters starting from start (depending on the length of the string ).
If a negative length is provided, many characters at the end of the string will be missed (if start is a negative number, it will be counted from the end of the string ). If start is not in this text, an empty string is returned.
If the length is 0, FALSE, or NULL, an empty string is returned.
If length is not provided, the returned substring starts from the start position until the end of the string.
Example #2 use a negative length
<? Php $ rest = substr ("abcdef", 0,-1); // return "abcde" $ rest = substr ("abcdef", 2,-1 ); // return "cde" $ rest = substr ("abcdef", 4,-4); // return "" $ rest = substr ("abcdef",-3, -1); // return "de"?>
Return Value
Returns the extracted substring, or FALSE if it fails.
Update log version description
5.2.2-5.2.6 If the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start.
Example
Example #3 substr () basic usage
<? Phpecho substr ('abcdef', 1); // bcdefecho substr ('abcdef', 1, 3); // bcdecho substr ('abcdef', 0, 4 ); // abcdecho substr ('abcdef', 0, 8); // abcdefecho substr ('abcdef',-1, 1 ); // f // a single character in the access string // you can also use brackets $ string = 'abcdef'; echo $ string [0]; // aecho $ string [3]; // decho $ string [strlen ($ string)-1]; // f?>
Example #4 substr () casting behaviour
<?phpclass apple {public function __toString() {return “green”;}}echo “1) “.var_export(substr(“pear”, 0, 2), true).PHP_EOL;echo “2) “.var_export(substr(54321, 0, 2), true).PHP_EOL;echo “3) “.var_export(substr(new apple(), 0, 2), true).PHP_EOL;echo “4) “.var_export(substr(true, 0, 1), true).PHP_EOL;echo “5) “.var_export(substr(false, 0, 1), true).PHP_EOL;echo “6) “.var_export(substr(“”, 0, 1), true).PHP_EOL;echo “7) “.var_export(substr(1.2e3, 0, 4), true).PHP_EOL;?>
The above routine will output:
1) 'pe'
2) '54'
3) 'gr'
4) '1'
5) false
6) false
7) '123'
Error/exception
FALSE is returned when an error occurs.
<?phpvar_dump(substr(‘a', 1)); // bool(false)?>
The above example of php getting the first few digits of the string (substr returns the usage of the substring of the string) is all the content shared by the editor. I hope to give you a reference, we also hope that you can support the customer's home.