How to remove all leading zeroes in a string

If I have a string


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


note that the number above was generated randomly.



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


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

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

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

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

echo $a; // will output 1

you can add "+" in your variable,

example :

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

Regex was proposed already, but not correctly:

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

output is then:


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.

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.

 $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";


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

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;

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, don't remember where.

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?

Need Your Help

Spring - replace @Service bean in test

spring testing service configuration javabeans

In my spring boot application I have a service bean (with @Service annotation) and I want to mock this service in particular JUnit test (not in all tests). How can I replace this service bean for j...

Is tail recursion merely a special case of CPS?

javascript tail-recursion continuations

I've implemented map in tail recursive and continuation passing style. Both versions are quite similar: