php 将 01, 02, 03, 04 .. 09 转换为 1,2,3,4 ... 9
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1847544/
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
convert 01, 02, 03, 04 .. 09 to 1,2,3,4 ... 9
提问by dotty
Hay how can I convert
嘿,我该如何转换
01 to 1
02 to 2
all the way to 9?
一直到9?
Thanks
谢谢
回答by Doug T.
I assume the input is a string?
我假设输入是一个字符串?
$str = "01";
$anInt = intval($str);
You may think that the leading 0 would mean this is interpreted as octal, as in many other languages/APIs. However the second argument to intval is a base. The default value for this is 10. This means 09->9. See the first comment at the intvalpage, which states that the base deduction you might expect only happens if you pass 0 in as the base.
您可能认为前导 0 表示这被解释为八进制,就像在许多其他语言/API 中一样。然而, intval 的第二个参数是一个基数。默认值为 10。这意味着 09->9。请参阅intval页面上的第一条评论,其中指出您可能期望的基数扣除仅在您将 0 作为基数传入时才会发生。
回答by YOU
$x="01";
$x=+$x;
$x="02";
$x=+$x;
...
or
或者
$x=+"01";
should work for both int, and string
应该适用于 int 和 string
回答by Jasper Poppe
Do (int)$str;instead. It's up to 4x faster than intval().
做(int)$str;代替。它比 快 4 倍intval()。
回答by Vi J
you can use the predefined php function .
您可以使用预定义的 php 函数。
intval()
For Ex:
例如:
$convert = intval(01);
echo $convert ;
It will print 1(one);
它将打印 1(one);
回答by Vitali
If you want to use the generic regular expression solution: 's/[0]([0-9])/\1/' (add in anchors as appropriate)
如果要使用通用正则表达式解决方案: 's/[0] ([0-9])/\1/' (根据需要添加锚点)
回答by hmt
$str='05';
if(strpos($str,'0')==0 && strpos($str,'0') != false ){
$new = intval(substr($str,1));
}
回答by Fortega
$str = "05";
$last = $str[1];
回答by erenon
$i = substr($input, 1, 1);

