Home >Backend Development >PHP Tutorial > 请问一个str_replace函数的有关问题

请问一个str_replace函数的有关问题

WBOY
WBOYOriginal
2016-06-13 13:45:23890browse

请教一个str_replace函数的问题
有个问题还要请教下,比如下面一组数据

00010,00014,0000000023,24,0027,0000

我想去掉这里面的所有的0 , 但是“,”号前面的0不去掉



00010,00014,0000000023,24,0027,0000

这组数据变成

10,14,23,24,27

请问这个该怎么做呢?

------解决方案--------------------
echo preg_replace('/(^|,)0+/', '$1', '00010,00014,0000000023,24,0027,0000');

10,14,23,24,27,

1、str_replace 只能替换固定的字符串,不定的串需要用正则表达式
2、不认为 10,14,23,24,27 这个结果是正确的,因为他丢失了一项
------解决方案--------------------

PHP code

$str    = "00010,00014,0000,000023,24,0027,0000";
echo    trim(preg_replace("/[^,]+,?/e", "intval('\\0') > 0 ? intval('\\0').',' : ''", $str), ',');
//10,14,23,24,27 <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn