Measuring the length of a integer ( when interpreted as octal number)

138 views Asked by At

I need to know how many characters / numbers are in a number. Some of the numbers will have leading Zeros. After much reading I understand that if it has leading zeroes PHP translates this into an octal number. But I need a way to void that and treat the number as a normal number. also I need to be able to print this to the screen with the leading zeros later.

Is there any straight forward work around for this?

I dont want the octal number. I want it to know is 00123 or at least 123 Not 83.

Idealy my desired output is 3 or 5 when counting the characters in the number. and is a number not a string to begin with

$a = 00123;
echo $a; // Returns 83
echo strlen((string)$a); // Returns 2
echo mb_strlen($a, "UTF-8"); //Returns 2

$b = "". 00123 ."";
echo strlen((string)$b);  //Returns 2
echo strlen(strval($b)); //Returns 2
2

There are 2 answers

0
Anthony Sterling On BEST ANSWER

You can use a combination of decoct and str_pad, like so. http://3v4l.org/TQgOq

<?php
echo str_pad(decoct(000123), 20, '0', STR_PAD_LEFT);

You may want to sanity check some of the incoming values though.

0
Realitätsverlust On

Heres the whole answer, i dont like code in comments :3

$a = "00123";
echo $a; // Returns 00123
echo strlen($a); // Returns 5
echo mb_strlen($a, "UTF-8"); //Returns 5

$b = "". 00123 .""; //unnecessary
$b = $a; //easier
echo strlen((string)$b);  //both
echo strlen(strval($b)); //unnecessary
echo strlen($b); //returns 5