Home > Article > Backend Development > How to remove 0 from string in php
Method: 1. Use "trim($str,"0")" to remove the 0's at both ends of the string; 2. Use "str_replace("0","",$str)" to remove Remove all 0s from the string; 3. Use "substr_replace($str,"",position value,1)" to remove 0s at the specified position.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php removes strings 0
1. Use the trim() function
Use the trim() function to remove the 0
<?php header('content-type:text/html;charset=utf-8'); $str = "0dfd0125e0reg0"; echo "原字符串:".$str; echo "<br>处理后:".trim($str,"0"); ?># at both ends of the string.
##2. Use the str_replace() function
Use the str_replace() function to remove all 0<?php header('content-type:text/html;charset=utf-8'); $str = "0dfd0125e0reg0"; echo "原字符串:".$str; echo "<br>处理后:".str_replace("0","",$str); ?>from the string
3. Use the substr_replace() function
Use the substr_replace() function to remove 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>"; ?>## at the specified position in the string
# Recommended learning: "
PHP Video TutorialThe above is the detailed content of How to remove 0 from string in php. For more information, please follow other related articles on the PHP Chinese website!