Home  >  Article  >  Backend Development  >  How to remove zeros from the end of a value in php

How to remove zeros from the end of a value in php

青灯夜游
青灯夜游Original
2022-05-30 19:36:353504browse

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.

How to remove zeros from the end of a value in php

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");
?>

How to remove zeros from the end of a value in php

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,&#39;&#39;, -1)."<br>";
echo "去0处理后:".substr_replace($num,&#39;&#39;, -2)."<br>";
echo "去0处理后:".substr_replace($num,&#39;&#39;, -3)."<br>";
echo "去0处理后:".substr_replace($num,&#39;&#39;, -4)."<br>";
?>

How to remove zeros from the end of a value in php

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!

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