Jump to content

[SOLVED] Problems with str_pad when used with implode


tgpo

Recommended Posts

I'm attempting to create a script that will display all numbers from 0 - 9999 on a single line.

 

All numbers need to be 4 digits and comma separated.

 

I thought I figured it out, but str_pad simply isn't doing its job when used in conjunction with implode.

 

Here is the code I'm using.

 

<?=ltrim(chunk_split(str_pad(implode(range(0, 9999)), 4, "0", STR_PAD_LEFT), 4,", "), ', ');?>

 

Why doesn't str_pad pad the rangle numbers with the 0s?

Link to comment
Share on other sites

<?php
$nums = range(0, 9999);
foreach($nums as $key=>$num)
   $nums[$key] = sprintf('%04d', $num);
}
echo implode(',', $nums);
?>

 

Don't use short tags.  Please.

 

Short tags are only in use because the goal is to only use single line of code.

Link to comment
Share on other sites

...Why?  You don't need short tags regardless of whether or not it should be one line.  Don't use them.  Why in the hell does it have to be one line?  I mean, since whitespace means nothing, you could do something that's UGLY like:

 

<?php $nums=range(0,9999);foreach($nums as $key=>$num) { $nums[$key] = sprintf('%04d', $num); } echo implode(',', $nums); ?>

 

But how ugly is that?

 

EDIT: @CV: He wants them implode()'d with a comma, so it's easier than appending to a variable and then stripping off the last comma with substr().

Link to comment
Share on other sites

I don't think he necessarily cares about whether it's imploded or not...he didn't really specify his overall goal here.  He was just using implode in his own code to get things to work.  I don't really see how doing a substr or rtrim would be any more difficult but w/e.  Anyways...

 

Don't use short tags.  Please.

 

Short tags are only in use because the goal is to only use single line of code.

 

The reason you shouldn't use short tags is because they are not universally compatible with all server setups, and that may or many not change in the future.  Your script may work fine today on your server, but it may not tomorrow, due to not adding on 3 little letters.

Link to comment
Share on other sites

and what's the point of wanting it on one line, honestly, tgpo?

 

A friend and I were going back and forth today refining each other's code to display numbers 0000-9999.  We started with 14 lines, then I edited it to 13, then to 10, then 9, etc.

 

He got it to 3 lines.  I thought I got it to 1 with the code at the top.  But for some reason str_pad isn't doing its job.

 

This is just for fun and a "Can it be done" competition.  I suppose if you wanted to see the longer history you could see the entire log of our back and forth on Twitter, but that's it in a nutshell.

Link to comment
Share on other sites

...Why?  You don't need short tags regardless of whether or not it should be one line.  Don't use them.  Why in the hell does it have to be one line?  I mean, since whitespace means nothing, you could do something that's UGLY like:

 

<?php $nums=range(0,9999);foreach($nums as $key=>$num) { $nums[$key] = sprintf('%04d', $num); } echo implode(',', $nums); ?>

 

 

I would count this as more than one command line.  One ends after declaring $nums, two ends at the end of the foreach statement, three ends with the echo statement.  I know we could take 1000 lines and put it on 1 line if we wanted to, but where's the challenge in that?

Link to comment
Share on other sites

I'm attempting to create a script that will display all numbers from 0 - 9999 on a single line.

 

All numbers need to be 4 digits and comma separated.

 

I thought I figured it out, but str_pad simply isn't doing its job when used in conjunction with implode.

 

Here is the code I'm using.

 

<?=ltrim(chunk_split(str_pad(implode(range(0, 9999)), 4, "0", STR_PAD_LEFT), 4,", "), ', ');?>

 

Why doesn't str_pad pad the rangle numbers with the 0s?

 

str_pad is doing it's job. You're only padding the resulting string from implode(), not each array element.

 

I don't think this is possible without a loop.

 

for ($x = 0;$x < 10000;$x++) echo sprintf("%04d,",$x);

Link to comment
Share on other sites

tgpo, str_pad expects the second param to be the length of your original string plus what you want to add to it so

 

this works

<?=str_pad(implode(range(0, 9999)), (strlen(implode(range(0, 9999))))+2, "0-", STR_PAD_LEFT); ?>

 

this doesn't

<?=str_pad(implode(range(0, 9999)), 2, "0-", STR_PAD_LEFT); ?>

 

Still trying to figure out a way to get this on entire thing on one line. It's tough without the first 999 have the 0's in front of them. If it truly did 0000 to 9999 in the range, this thing would be simple. str_pad won't work as it pads the string, not each chunk created by chunk_split.

 

crap, discomatt posted as I was typing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.