简体   繁体   English

如何删除字符串中的所有前导零

[英]How to remove all leading zeroes in a string

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.请注意,上面的数字是随机生成的。

ltrim :

$str = ltrim($str, '0');
(string)((int)"00000234892839")

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:或者按照建议(从 PHP 5.3 开始),可以使用速记三元运算符:

$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/ ):给你,最简单和最简单的方法(如下解释: https : //nabtron.com/kiss-code/ ):

$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:已经提出了正则表达式,但不正确:

<?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.因此,正则表达式^0+匹配字符串开头的所有零。

I don't think preg_replace is the answer.. old thread but just happen to looking for this today.我不认为 preg_replace 是答案.. 旧线程,但碰巧今天正在寻找这个。 ltrim and (int) casting is the winner. ltrim 和 (int) 铸造是赢家。

<?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

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

一个简短的技巧可以是使用 round() 它将删除前导零。

echo round('00020300504'); //20300504

Ajay Kumar offers the simplest echo +$numString; Ajay Kumar 提供了最简单的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.我在 php.net 上看到的,不记得在哪里了。

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?这个帮助?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM