方法:1、使用“str_replace(".","",$str)”语句,可查找小数点并将其替换成空字符;2、用“substr_replace($str,"",stripos($str,"."),1)”语句,根据小数点的位置将其替换成空字符。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

php字符串去掉小数点的方法

方法1:使用str_replace() 函数

可以利用str_replace() 函数查找小数点“.”并将其替换成空字符""即可。

示例:

<?php
header('content-type:text/html;charset=utf-8');   
$str = "123.567";
echo "原字符串:".$str."<br>";
$new = str_replace(".","",$str);
echo "新字符串:".$new;
?>

1.png

方法2:使用stripos()+substr_replace() 函数

  • 使用stripos()获取小数点“.”的位置

  • 使用substr_replace()根据获取的位置将小数点“.”替换成空字符""即可。

示例:

<?php
header('content-type:text/html;charset=utf-8');   
$str = "3.1415";
echo "原字符串:".$str."<br>";
$new = substr_replace($str,"",stripos($str,"."),1);
echo "新字符串:".$new;
?>

2.png


php字符串怎么去掉小数点