方法:1、使用“str_replace(".","",$str)”語句,可查找小數(shù)點并將其替換成空字符;2、用“substr_replace($str,"",stripos($str,"."),1)”語句,根據(jù)小數(shù)點的位置將其替換成空字符。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php字符串去掉小數(shù)點的方法
方法1:使用str_replace() 函數(shù)
可以利用str_replace() 函數(shù)查找小數(shù)點“.”并將其替換成空字符""
即可。
示例:
<?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() 函數(shù)
-
使用stripos()獲取小數(shù)點“.”的位置
-
使用substr_replace()根據(jù)獲取的位置將小數(shù)點“.”替換成空字符""即可。
示例:
<?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視頻教程》