dotty
dotty

Reputation: 41473

convert 01, 02, 03, 04 .. 09 to 1,2,3,4 ... 9

Hay how can I convert

01 to 1
02 to 2

all the way to 9?

Thanks

Upvotes: 5

Views: 25752

Answers (8)

Vi J
Vi J

Reputation: 39

you can use the predefined php function .

intval() 

For Ex:

 $convert =  intval(01);
 echo $convert ;

It will print 1(one);

Upvotes: 0

Jasper Poppe
Jasper Poppe

Reputation: 521

Do (int)$str; instead. It's up to 4x faster than intval().

Upvotes: 3

$str='05';

if(strpos($str,'0')==0 && strpos($str,'0') != false ){ 
 $new = intval(substr($str,1));
}

Upvotes: 0

Vitali
Vitali

Reputation: 3695

If you want to use the generic regular expression solution: 's/[0]([0-9])/\1/' (add in anchors as appropriate)

Upvotes: 0

YOU
YOU

Reputation: 123881

$x="01";
$x=+$x;

$x="02";
$x=+$x;

...

or

$x=+"01";

should work for both int, and string

Upvotes: 2

Doug T.
Doug T.

Reputation: 65639

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 intval page, which states that the base deduction you might expect only happens if you pass 0 in as the base.

Upvotes: 26

erenon
erenon

Reputation: 19118

$i = substr($input, 1, 1);

Upvotes: 0

Fortega
Fortega

Reputation: 19702

$str = "05";
$last = $str[1]; 

Upvotes: 0

Related Questions