jwhite68 Posted May 29, 2007 Share Posted May 29, 2007 I am trying to simply pad a string with spaces (to the right) so that the total length of the string is 125 characters. $detail2="ab"; $descr = str_pad($detail2,125,' '); echo 'descr=' . $descr . 'end'; If I run this, then the padding does not pad the string with spaces. However, if I change the ' ' to a '-' it correctly pads with hyphens. So it suggests the syntax is correct - but maybe there is an issue padding with blank spaces? I did try without the 3rd argument on the str_pad too, as it should be if you just want to pad with spaces, but that doesnt work either. Am I missing some quirky setting in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/ Share on other sites More sharing options...
taith Posted May 29, 2007 Share Posted May 29, 2007 more then likely no... html shrinks 2 spaces into 1... so you put 125 spaces, and it would only show one... you'd need to use Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263826 Share on other sites More sharing options...
jwhite68 Posted May 29, 2007 Author Share Posted May 29, 2007 Any idea how. If I just put   in the str_pad function, it fails. Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263829 Share on other sites More sharing options...
taith Posted May 29, 2007 Share Posted May 29, 2007 $detail2="ab"; $descr = str_pad($detail2,125,' '); echo 'descr=' . $descr . 'end'; Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263830 Share on other sites More sharing options...
kenrbnsn Posted May 29, 2007 Share Posted May 29, 2007 Try this: <?php $descr = str_replace('~',' ',str_pad($detail2,125,'~')); // just use some character you know isn't in your string ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263831 Share on other sites More sharing options...
taith Posted May 29, 2007 Share Posted May 29, 2007 ken's right... as it pads upto the # of chars... mybad.... so it may pad &ns which wouldnt work... str_replace it in Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263832 Share on other sites More sharing options...
jwhite68 Posted May 29, 2007 Author Share Posted May 29, 2007 Strangely, taith, this is not producing the perfect result. Its not following the pad characters Ive asked for. $detail2 = "this is a test"; $descr = str_pad($detail2,125, " "); echo 'descr=' . $descr . 'end'; If you run this test you get this result: this is a test &nbenddate So its not padding the number of characters asked for, and its appending &nb in ther string. Weird.. Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263833 Share on other sites More sharing options...
jwhite68 Posted May 29, 2007 Author Share Posted May 29, 2007 Thanks Ken. Your code is working. But I am baffled why such a seamingly simple task needs this more complicated solution. I dont see why str_pad alone cant cut it? Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263836 Share on other sites More sharing options...
taith Posted May 29, 2007 Share Posted May 29, 2007 because... str_pad() increases by exact numeric value... so if it needs to crop 4 characters on, it'd add &nbs... not ... if it needs 2... it'd add &n... both of which, wouldnt work... Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263847 Share on other sites More sharing options...
jwhite68 Posted May 29, 2007 Author Share Posted May 29, 2007 The problem with   is that it adds unicode characters - and since I am encoding this string, I need it to be a single ASCII space. Any ideas on an alternative? Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263882 Share on other sites More sharing options...
jwhite68 Posted May 29, 2007 Author Share Posted May 29, 2007 I found the solution. Need <pre> tags as shown below: $descr = '<pre>' . str_replace('~',chr(32),str_pad($detail2,125,'~')) . '</pre>'; What a scary and complicated way to have to do something so seemingly straight forward. Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263932 Share on other sites More sharing options...
taith Posted May 29, 2007 Share Posted May 29, 2007 ya... browsers are picky... lol Quote Link to comment https://forums.phpfreaks.com/topic/53399-help-with-str_pad/#findComment-263950 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.