开发者

How to remove all leading zeroes in a string

开发者 https://www.devze.com 2023-02-13 14:47 出处:网络
If I have a string 00020300504 0000023开发者_高级运维4892839 000239074 how can I get rid of the leading zeroes so that I will only have this

If I have a string

00020300504
0000023开发者_高级运维4892839
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.


ltrim:

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


(string)((int)"00000234892839")


you can add "+" in your variable,

example :

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


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'; 


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


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.


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


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

echo round('00020300504'); //20300504


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.


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;
}


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消