Home > Article > Backend Development > How to remove zeros from the end of a value in php
Two removal methods: 1. Use the rtrim() function, the syntax "rtrim(value,"0")" to remove consecutive zeros after the value. 2. Use substr_replace(), the syntax "substr_replace(value,'', -position value)" to replace zeros with null characters starting from the specified position.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php will follow the value Zero removal method
Method 1: Use the rtrim() function
rtrim() function removes the blank characters on the right side of the string or Other predefined characters.
Use the rtrim() function to remove consecutive 0s on the right side of the value.
<?php header("Content-type:text/html;charset=utf-8"); $num=20340000; echo "原字符串:".$num."<br>"; echo "去0处理后:".rtrim($num,"0"); ?>
Method 2: Use the substr_replace() function
Use the substr_replace() function to replace all 0 characters starting at the specified position It is an empty character
<?php header("Content-type:text/html;charset=utf-8"); $num=20340000; echo "原字符串:".$num."<br>"; echo "去0处理后:".substr_replace($num,'', -1)."<br>"; echo "去0处理后:".substr_replace($num,'', -2)."<br>"; echo "去0处理后:".substr_replace($num,'', -3)."<br>"; echo "去0处理后:".substr_replace($num,'', -4)."<br>"; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove zeros from the end of a value in php. For more information, please follow other related articles on the PHP Chinese website!