Search code examples
phpsplitsubstrstrpos

Remove all zero values from string PHP


I have a string like this:

14522354265300000000000

I want to display it without zero values, how I can do this? I do this

$pos = strpos($route, '0');
$length = count(str_split($route));
$a = $length - $pos;
$a = substr($route, 0, $a);

but it remove 3 in the end of string. Can somebody help me?

Additional: If string will be 123088888880, I want make it 123.


Solution

  • here's a nice algo:

    <?php
    
    $string = "14522354265300000000000";
    $new_string = '';
    for($i=0; $i<strlen($string) ; $i++){
        if($string[$i] != '0'){
            $new_string .= $string[$i];
        }
    }
    
    echo $new_string;
    
    ?>
    

    rtrim is only if you have zero's at end of string :)