Jump to content

Help with str_pad


jwhite68

Recommended Posts

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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

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.