方法: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
<?phpheader('content-type:text/html;charset=utf-8'); $str = "0dfd0125e0reg0";echo "原字符串:".$str;echo "<br>处理后:".trim($str,"0");?>
2、使用str_replace()函数
利用str_replace()函数可以去除字符串中的全部0
<?phpheader('content-type:text/html;charset=utf-8'); $str = "0dfd0125e0reg0";echo "原字符串:".$str;echo "<br>处理后:".str_replace("0","",$str);?>
3、使用substr_replace() 函数
利用substr_replace() 函数可以去除字符串指定位置的0
<?phpheader('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>";?>
推荐学习:《php视频教程》
以上就是php怎么去掉字符串的0的详细内容。