php字符串怎么去掉小数点

php字符串怎么去掉小数点,第1张

php字符串怎么去掉小数点

方法: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;
?>

方法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;
?>

推荐学习:《PHP视频教程》

以上就是php字符串怎么去掉小数点的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/731343.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存