We help IT Professionals succeed at work.

How to avoid zero(s) in front of number(s) disappears in array

trrsrr
trrsrr asked
on
Hi experts...
I included my simplified codes below.

Straight to the example so you can get a better view for this case :

Let say I have $data = '1-5';
and my code turns it correctly into:
1
2
3
4
5

But if I have $data = '01-05';
then my code turns it into (not correct, not what I want):
01
2
3
4
5

... where the correct result (that I want) should be :
01
02
03
04
05

And the results are always not correct if I have :
$data = '01-05';
or...
$data = '0021-0025';
or maybe...
$data = '000101-000108';

So, seems like zero(s) in front of the numbers are disappear.. only the top/first array that is displayed correctly.

Any advance help for this case please ...
<?php

$data = '1-5';
//$data = '01-05';
//$data = '0021-0025';
//$data = '000101-000108';

	list($start_at, $end_at) = split('-', $data); 

	$arr_data = ''; 
	for ($i = $start_at; $i <= $end_at; $i++) 
		{
			$arr_data .= $i.'<br>';
		}

echo $arr_data;

?>

Open in new window

Comment
Watch Question

The code didn't attach properly.
<?php

$data = '1-5';
//$data = '01-05';
//$data = '0021-0025';
//$data = '000101-000108';

     list($start_at, $end_at) = split('-', $data); 

     $arr_data = ''; 
     for ($i = intval($start_at); $i <= intval($end_at); $i++) 
          {
               $arr_data .= $i.'<br>';
          }

echo $arr_data;

?>

Open in new window

Sorry, after posting the above I decided I had mis-read the question and that the snippet below is nearer what you need.

It's obviously too late here.....  :-(


<?php

$data = '1-5';
$data = '01-05';
$data = '0021-0025';
$data = '000101-000108';

     list($start_at, $end_at) = split('-', $data); 
     $formatStr = "%0" . strlen($start_at) . "d";

     $arr_data = ''; 
     for ($i = intval($start_at); $i <= intval($end_at); $i++) 
          {
               $arr_data .= sprintf( $formatStr, $i ).'<br>';
          }

echo $arr_data;

?>

Open in new window

Author

Commented:
@bportlock:
I believe you got my point wrong, I dont want to remove all available zero(s) ... I want those available zero(s) to appears.

So, for example I have $data = '000101-000105';
Then the result should be:
000101
000102
000103
000104
000105

while your codes will give me this result:
101
102
103
104
105

Author

Commented:
Ha ha ... u're cool man.

Thanks for the revised codes. It's awesome...  ^_^
Hey - glad it's OK for you. I'm off for a good night's kip.

Good luck with your code & project.