Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Str_pad only working when padding is not a space

    Hi,

    I'm padding out entries in an array to the length of the last entry (which will always be the longest).

    I want the padding to be a space as it should be by default. The padding is only working if the character isn't a space.

    How can I fix this?

    PHP Code:
    <?php
    ...
          
    $packets = (count($sizes))-1;                      // Count the number of different packet sizes
    ...
          
    $i=0;
          
    $slength strlen($sizes[$packets]);                // Works out the length of the last entry in sizes
          
    $plength strlen($prices[$packets]);               // Works out the length of the last entry in prices
          
    $pad '_';
          while (
    $i <= $packets){
          
    $output3[] = '<b>'.str_pad($sizes[$i], $slength$pad STR_PAD_LEFT);           // Pads all packet sizes to the length of the longest one
    ...
          
    $output3[] = '£'.str_pad($prices[$i], $plength$pad STR_PAD_LEFT);            // Pads all packet prices to the length of the longest one
    ...
          
    $i++;
          }
    ...
    ?>
    If I put a space into the string that is to be used to pad out the entries nothing happens, no padding whatsover.

    Any help would be appreciated!
    Thanks

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    try
    PHP Code:
    <?$pad='&nbsp;';?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    Edit: too slow again, basically says the same as above ^^

    If you are outputting this to a browser, spaces mean nothing as far as formatting and alignment goes (unless you are using something like the <pre></pre> tags). Groups of spaces are condensed to a single space. To do what you want, you need to use a non-breaking-space '&nbsp;' (use all the characters between the single-quotes.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New Coder
    Join Date
    May 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'd already tried &nbsp; without the quotes which had caused an error (forgot to mention that).

    Doing it with quotes literally adds that as the padding rather than the space.

    Hm, ok I've just outputted the length of the one that should have been padded, and you are right, it has been padded with the space and the browser just isn't showing it.

    Thats kind of annoying :-(

    I think I may have a way to get around it though...

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    Instead of using lengths of strings to align columns of values, use some text alignment properties - http://w3schools.com/css/tryit.asp?f...css_text-align
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    New Coder
    Join Date
    May 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did try doing that yesterday but in this instance it wasn't suitable.

    Anyway I got it to work with a bit of clever thinking

    The string is padded out and then the padded character is replaced with two empty spaces.

    It works perfectly!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •