php 如何删除字符串中的所有前导零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5098688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove all leading zeroes in a string
提问by Gerald
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.
请注意,上面的数字是随机生成的。
回答by Svisstack
(string)((int)"00000234892839")
回答by Nabeel Khan
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
回答by Mike Weir
Similar to another suggestion, except will not obliterate actual zero:
类似于另一个建议,除了不会抹去实际的零:
if (ltrim($str, '0') != '') {
$str = ltrim($str, '0');
} else {
$str = '0';
}
回答by Anthony Kal
you can add "+" in your variable,
您可以在变量中添加“+”,
example :
例子 :
$numString = "0000001123000";
echo +$numString;
回答by Fuujin
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+
匹配字符串开头的所有零。
回答by Elcast
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.
我不认为 preg_replace 是答案.. 旧线程,但碰巧今天正在寻找这个。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
回答by Ali Sufyan
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;
}
回答by Jorge Garza
Ajay Kumar offers the simplest echo +$numString;I use these:
Ajay Kumar 提供了最简单的echo +$numString;我使用这些:
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 上看到的,不记得在哪里了。
回答by Will
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?
这个帮助?