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

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

青灯夜游
Release: 2023-03-16 09:24:02
Original
3561 people have browsed it

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

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>";
?>
Copy after login

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!

Related labels:
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