Home >Backend Development >PHP Problem >How to remove the last four digits of a string in PHP?

How to remove the last four digits of a string in PHP?

Guanhui
GuanhuiOriginal
2020-06-12 16:53:204787browse

How to remove the last four digits of a string in PHP?

#How to remove the last four digits of a string in PHP?

In PHP, you can use the "substr()" function to remove the last four digits of a string. This function will return the substring of the string. Its syntax is "substr(str,start,length)" , when using, pass the string to be operated on to the parameter str, start is 0, and length is the length of the string minus 4.

Sample code

 
<? 
echo substr("abcdef", 1, 3); // 返回 "bcd" 
echo substr("abcdef", -2); // 返回 "ef" 
echo substr("abcdef", -3, 1); // 返回 "d" 
echo substr("abcdef", 1, -1); // 返回 "bcde" 
?>
 
$str = "1,2,3,4,5,6,"; 
$newstr = substr($str,0,strlen($str)-1); 
echo $newstr;

Recommended tutorial: "PHP"

The above is the detailed content of How to remove the last four digits of a string in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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