-2

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.

nowiko
  • 2,507
  • 6
  • 38
  • 82
  • `rtrim($string, '0');` – jeroen Mar 10 '15 at 13:10
  • http://php.net/manual/en/function.trim.php http://php.net/manual/en/function.rtrim.php – Brian Moore Mar 10 '15 at 13:11
  • 2
    *"Additional: If string will be 123088888880, I want make it 123."* - *Oh,* the plot thickens, [from your original post](http://stackoverflow.com/revisions/28964543/1). I had a feeling they'd be more to this from the *get go*. Ah, human instincts, *eh?!* – Funk Forty Niner Mar 10 '15 at 13:17
  • So you don't want to remove all zero values but cut the string before the first 0? Use `explode('0', $route, 1)` then – kero Mar 10 '15 at 13:19

3 Answers3

3

You can use rtrim for this:

echo rtrim("14522354265300000000000", "0"); // outputs: 145223542653
Dan Smith
  • 5,685
  • 32
  • 33
2

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 :)

Yair.R
  • 795
  • 4
  • 11
1

You can use rtrim('14522354265300000000000', '0')

Daniel Mensing
  • 956
  • 5
  • 13