In PHP, you can use the substr() function to read the last few characters of a string. You only need to set the second parameter of the function to a negative value and omit the third parameter; syntax "substr(string,-n)" means reading all characters from the nth character forward from the end of the string to the end of the string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use substr () function to read the last few characters of the string.
substr() function returns a part of a string.
You only need to omit the third parameter of the function, and set the second parameter to a negative value (-n), n specifies the last few characters you want to read
Implementation example :
<?php header('content-type:text/html;charset=utf-8'); $str = "abcdefg"; echo "原字符串:".$str."<br>"; echo "读取后1个字符:".substr($str,-1)."<br>"; echo "读取后2个字符:".substr($str,-2)."<br>"; echo "读取后3个字符:".substr($str,-3)."<br>"; ?>
Description:
substr() function can intercept a certain length of characters from the specified position of the string. This paragraph The intercepted characters can be called "substrings" or "substrings", and their syntax format is as follows:
substr($string, $start [, $length])
Parameter description is as follows:
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to read the last few characters of a string in php. For more information, please follow other related articles on the PHP Chinese website!