方法:1、用“trim($str,"0")”,可去掉字符串两端的0;2、用“str_replace("0","",$str)”,可去除字符串的全部0;3、用“substr_replace($str,"",位置值,1)”,可去除指定位置的0。

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

php去掉字符串的0

1、使用trim()函数

利用trim()函数可以去掉字符串两端的0

<?php
header('content-type:text/html;charset=utf-8');   
$str = "0dfd0125e0reg0";
echo "原字符串:".$str;
echo "<br>处理后:".trim($str,"0");
?>

1.png

2、使用str_replace()函数

利用str_replace()函数可以去除字符串中的全部0

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

2.png

3、使用substr_replace() 函数

利用substr_replace() 函数可以去除字符串指定位置的0

<?php
header('content-type:text/html;charset=utf-8');   
$str = "0dfd0125e0reg0";
echo "原字符串:".$str."<br><br>";
echo "去除开头的0:".substr_replace($str,"",0,1)."<br><br>";
echo "去除末尾的0:".substr_replace($str,"",-1,1)."<br><br>";
echo "去除第5字符0:".substr_replace($str,"",4,1)."<br><br>";
echo "去除第10字符0:".substr_replace($str,"",9,1)."<br><br>";
?>

3.png


php怎么去掉字符串的0