tgpo Posted September 3, 2008 Share Posted September 3, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/ Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 <?php $nums = range(0, 9999); foreach($nums as $key=>$num) $nums[$key] = sprintf('%04d', $num); } echo implode(',', $nums); ?> Don't use short tags. Please. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633137 Share on other sites More sharing options...
tgpo Posted September 3, 2008 Author Share Posted September 3, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633141 Share on other sites More sharing options...
.josh Posted September 3, 2008 Share Posted September 3, 2008 If you're just wanting to print 0-9999 in a specific format, I don't really see the point in using range or foreach loop...just use a plain old loop... for ($x = 0;$x < 10000;$x++){ echo sprintf("%04d,",$x); } Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633147 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 ...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(). Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633150 Share on other sites More sharing options...
.josh Posted September 3, 2008 Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633152 Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 And "echo". Don't forget those extra 4 letters and a space, CV. Deadly mistake. xD Anyway, yeah. Short tags aren't a good design choice, and what's the point of wanting it on one line, honestly, tgpo? Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633154 Share on other sites More sharing options...
tgpo Posted September 3, 2008 Author Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633158 Share on other sites More sharing options...
tgpo Posted September 3, 2008 Author Share Posted September 3, 2008 ...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? Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633165 Share on other sites More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633213 Share on other sites More sharing options...
jtkendall Posted September 3, 2008 Share Posted September 3, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633232 Share on other sites More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 I cheated a bit, but here it is, in one line echo preg_replace_callback('/\d++/',create_function('$m', 'return str_pad($m[0],4,0,STR_PAD_LEFT);'),implode(',',range(0,9999))); Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633256 Share on other sites More sharing options...
jtkendall Posted September 3, 2008 Share Posted September 3, 2008 I'd argue this is one command as there are no breaks ( until the end. Might be wrong, but I'd argue it. <? for($i=0; $i < 10000; $i++) echo ($i != 9999 ? substr(('0000'.$i), -4).', ' : substr(('0000'.$i), -4).''); ?> Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633261 Share on other sites More sharing options...
discomatt Posted September 4, 2008 Share Posted September 4, 2008 I personally wouldn't consider that to be a 'one-line' solution, as most devote a line to declaring a control structure. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633268 Share on other sites More sharing options...
tgpo Posted September 4, 2008 Author Share Posted September 4, 2008 str_pad is doing it's job. You're only padding the resulting string from implode(), not each array element. Oh. That makes sense. Crap, thought I had it too. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633281 Share on other sites More sharing options...
tgpo Posted September 4, 2008 Author Share Posted September 4, 2008 Got it! <?=rtrim(ltrim(str_replace(", 1", ", ",chunk_split(implode(range(10000, 19999)), 5,", ")), '1'), ', ');?> It won't win any awards for the most readable code, but it is 1 line. Quote Link to comment https://forums.phpfreaks.com/topic/122618-solved-problems-with-str_pad-when-used-with-implode/#findComment-633568 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.