Padding a string with blank spaces

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Padding a string with blank spaces

    I've tried both
    str_pad and str_repeat to insert blank spaces into a value.

    I want the string to be 34 characters long. I can do it using any other character than a blank space.

    Inserting a blank space truncates the value to 1 blank space.

    I have even tried inserting another character, then replacing that character with a blank and I still get the truncation.

    When I do a test on the string length,it returns the correct result, but the output does not show the blanks in the string.

    the code below demonstrates the problem.

    Code:
    //padding after
    $job = '12345';
    echo 'start-';
    $padLen = str_pad($job, 6,'_');
    echo $padLen;
    echo '-end';
    echo strlen($padLen)."<br>";
    echo 'start-';
    $padLen = str_pad($job, 10,'/');
    echo $padLen;
    echo '-end';
    echo strlen($padLen)."<br>";
    
    echo strlen($padLen)."~~~";
    $padLen = str_replace("/", " ", $padLen);
    echo $padLen;
    echo '-end';
    echo strlen($padLen)."<br>";
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    That's a function of HTML interpretation. Not of PHP. HTML automatically trims extra spaces when it is displayed to the user. If you need to a certain amount of spaces past 1, you need to use the HTML code for non-breaking spaces: &nbsp;

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      HTML always takes inserts 1 space for any whitespace that it finds and so you need to tell it not to. In order to do that you need to specify that your spaces are special and should be rendered as spaces. To do that you use the HTML Entity &nbsp; (see the link for more info).

      So, in your server-side (php) code, you need to convert blank spaces to the HTML value of &nbsp in order for the spaces to appear in the browser.

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        Ok I get that, but I'm not trying to display the value. I'm trying to store it in MySQL table.

        I realize I did not make that clear in my statement above.

        I have been testing the values and length using firephp which returns the value in the header not inside the HTML tags.

        I will test per your suggestion and report stored results.

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Also if you are padding the middle of the string even HTML should not truncate the value. It was my understanding that spaces would only be trimmed from the front and back of a string.

          Am I wrong on that assumption?

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Yes, you are wrong on that assumption. HTML doesn't care where the consecutive whitespaces are located, it will reduce it to 1.

            Comment

            • Claus Mygind
              Contributor
              • Mar 2008
              • 571

              #7
              Ok I will accept that.

              Please run this code and tell me why with the the two strings are not the same length. Both should have a length of 34 characters. but the str_pad() string is only 24 characters.

              Code:
              	$n = 'abcd';
              	$x = '1234567890';
              	
              	echo 'length of x = '.strlen($x).'<br>';
              
              	$padLength = 30-strlen($x);
              	echo 'the length to pad is '.$padLength.'<br>';
              
              	echo 'the values for both <b>$newString</b> and <b>$loopPad</b> should be the same 34 chararcter long.<br>';
              
              	$newString = str_pad($x, $padLength," ",STR_PAD_RIGHT).$n;
              	echo '('.$newString.') the length for this <b>$newString</b> is '.strlen($newString).'<br>';
              
              	$loopPad = $x;
              	for ($i=0; $i<$padLength; $i++)
              	{
              		$loopPad .= ' ';
              	}
              	$loopPad .= $n;
              	echo '('.$loopPad.') the length of <b>$loopPad</b> is'.strlen($loopPad);

              Comment

              • Claus Mygind
                Contributor
                • Mar 2008
                • 571

                #8
                Note - I altered the code on line 12 to
                Code:
                $newString = [B]$x.[/B]str_pad($x, $padLength," ",STR_PAD_RIGHT).$n;
                That gave me 34 characters but the initial string was repeated
                (12345678901234 567890 abcd) the length for this $newString is 34

                So I'm not quite sure how str_pad() is suppose to work.
                Last edited by Claus Mygind; Nov 3 '14, 06:57 PM. Reason: Highlight change in code with bold

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  When you call the str_pad method as a method of a string object (as opposed to calling the function by itself), the first parameter is the string to use to pad, not the string that needs padding. That's why it's repeating.

                  The length parameter of the str_pad function is the total length of the output string, not how many characters to add, that's why you only get a length of 24.

                  Comment

                  • Claus Mygind
                    Contributor
                    • Mar 2008
                    • 571

                    #10
                    This was the piece of information I misread in the online manual. Thanks Rabbit for the clarification.

                    The length parameter of the str_pad function is the total length of the output string, not how many characters to add, that's why you only get a length of 24.

                    Code:
                    /*
                    WRONG way to do it. I calcualted the difference between the input string and the desired length and used that number to pad the string.
                    */
                     [B]$padLength[/B] = 30-strlen($myInputString);
                     $newString = str_pad($myInputString, [B]$padLength[/B]," ",STR_PAD_RIGHT);
                    
                    /*
                    CORRECT way to do it.  Don't do any calculation just let the function do the work.
                    */
                    $newString = str_pad($myInputString, [B]$desired_Length[/B]," ",STR_PAD_RIGHT);
                    Last edited by Claus Mygind; Nov 6 '14, 12:33 PM. Reason: To give a working example of the answer given to the question asked

                    Comment

                    Working...