How to remove all leading zeroes in a string

PhpString

Php Problem Overview


If I have a string

00020300504
00000234892839
000239074

how can I get rid of the leading zeroes so that I will only have this

20300504
234892839
239074

note that the number above was generated randomly.

Php Solutions


Solution 1 - Php

ltrim:

$str = ltrim($str, '0');

Solution 2 - Php

(string)((int)"00000234892839")

Solution 3 - Php

Similar to another suggestion, except will not obliterate actual zero:

if (ltrim($str, '0') != '') {
    $str = ltrim($str, '0');
} else {
    $str = '0';
}

Or as was suggested (as of PHP 5.3), shorthand ternary operator can be used:

$str = ltrim($str, '0') ?: '0'; 

Solution 4 - Php

Don't know why people are using so complex methods to achieve such a simple thing! And regex? Wow!

Here you go, the easiest and simplest way (as explained here: https://nabtron.com/kiss-code/ ):

$a = '000000000000001';
$a += 0;

echo $a; // will output 1

Solution 5 - Php

you can add "+" in your variable,

example :

$numString = "0000001123000";
echo +$numString;

Solution 6 - Php

Regex was proposed already, but not correctly:

<?php
    $number = '00000004523423400023402340240';
    $withoutLeadingZeroes = preg_replace('/^0+/', '', $number)
    echo $withoutLeadingZeroes;
?>

output is then:

4523423400023402340240

Background on Regex: the ^ signals beginning of string and the + sign signals more or none of the preceding sign. Therefore, the regex ^0+ matches all zeroes at the beginning of a string.

Solution 7 - Php

I don't think preg_replace is the answer.. old thread but just happen to looking for this today. ltrim and (int) casting is the winner.

<?php
 $numString = "0000001123000";
 $actualInt = "1123000";

 $fixed_str1 = preg_replace('/000+/','',$numString);
 $fixed_str2 = ltrim($numString, '0');
 $fixed_str3 = (int)$numString;

 echo $numString . " Original";
 echo "<br>"; 
 echo $fixed_str1 . " Fix1";
 echo "<br>"; 
 echo $fixed_str2 . " Fix2";
 echo "<br>";
 echo $fixed_str3 . " Fix3";
 echo "<br>";
 echo $actualInt . " Actual integer in string";

 //output

 0000001123000 Origina
 1123 Fix1
 1123000 Fix2
 1123000 Fix3
 1123000 Actual integer in tring

Solution 8 - Php

Im Fixed with this way.

its very simple. only pass a string its remove zero start of string.

function removeZeroString($str='')
{
    while(trim(substr($str,0,1)) === '0')
    {
        $str = ltrim($str,'0');
    }
    return $str;
}

Solution 9 - Php

A short hack can be to use round() it will remove leading zeros.

echo round('00020300504'); //20300504

Solution 10 - Php

Ajay Kumar offers the simplest echo +$numString; I use these:

echo round($val = "0005");
echo $val = 0005;
    //both output 5
echo round($val = 00000648370000075845);
echo round($val = "00000648370000075845");
    //output 648370000075845, no need to care about the other zeroes in the number
    //like with regex or comparative functions. Works w/wo single/double quotes

Actually any math function will take the number from the "string" and treat it like so. It's much simpler than any regex or comparative functions. I saw that in php.net, don't remember where.

Solution 11 - Php

Assuming you want a run-on of three or more zeros to be removed and your example is one string:

    $test_str ="0002030050400000234892839000239074";
    $fixed_str = preg_replace('/000+/','',$test_str);

You can make the regex pattern fit what you need if my assumptions are off.

This help?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeraldView Question on Stackoverflow
Solution 1 - PhplonesomedayView Answer on Stackoverflow
Solution 2 - PhpSvisstackView Answer on Stackoverflow
Solution 3 - PhpMike WeirView Answer on Stackoverflow
Solution 4 - PhpNabeel KhanView Answer on Stackoverflow
Solution 5 - PhpAnthony KalView Answer on Stackoverflow
Solution 6 - PhpFuujinView Answer on Stackoverflow
Solution 7 - PhpElcastView Answer on Stackoverflow
Solution 8 - PhpAli SufyanView Answer on Stackoverflow
Solution 9 - PhpHamza AliView Answer on Stackoverflow
Solution 10 - PhpJorge GarzaView Answer on Stackoverflow
Solution 11 - PhpWillView Answer on Stackoverflow