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
Release: 2023-03-01 14:54:02
Original
4752 people have browsed it

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" 
?>
Copy after login
 
$str = "1,2,3,4,5,6,"; 
$newstr = substr($str,0,strlen($str)-1); 
echo $newstr;
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template