Categories
PHP

Padding Strings

Pad a string on the left, on the right, or on both the left and the right using str_pad() and printf() functions.

Using str_pad() function

<?php
 str_pad(
  string $input,
  int $length,
  string $pad_string = " ",
  int $pad_type = STR_PAD_RIGHT
 ): string

str_pad function pad a string to a certain length with another string. This function accepts four parameters:

  1. input: The input string.
  2. length: No padding takes place if the length is negative, less than or equal to the input.
  3. pad_string: The output pad with the pattern specified by pad_string. If this parameter is not defined, the result will be padded with spaces.
  4. pad_type:
    • STR_PAD_RIGHT pad the string on the right (the default setting)
    • STR_PAD_LEFT pad the string on the left
    • STR_PAD_BOTH pad the string on both the left and the right, centering the result.

The following str_pad() basic example adds spaces to the end of the string:

<?php
 // prints "PHP" followed by three spaces
 echo str_pad("PHP", 6); //"PHP   "

By setting the optional argument pad_string, a string pattern is added at the end of the result:

<?php
 // prints "PHP" followed by three dots
 echo str_pad("PHP", 6, '.');
 //Prints: PHP...
 
 echo str_pad("PHP", 15, '-.-');
 //Prints: PHP-.--.--.--.-

By default, padding is added to the end of the string. By setting the optional argument pad_type to STR_PAD_LEFT or to STR_PAD_BOTH, the padding is added to the beginning of the string or to both ends.

<?php
 echo str_pad("PHP", 15, '-=') .'<br>';
 //PHP-=-=-=-=-=-=
 
 echo str_pad("PHP", 15, '-=', STR_PAD_LEFT) .'<br>';
 //-=-=-=-=-=-=PHP
 
 echo str_pad("PHP", 15, '-=', STR_PAD_BOTH);
 //-=-=-=PHP-=-=-=

The following example shows how str_pad( ) can create a justified index (or a table of contents):

<?php
 echo '<pre>';
 
 echo str_pad('Table of Contents', 50, ' ', STR_PAD_BOTH) . "\n";

 echo str_pad('Topic', 30, ' ');
 echo str_pad('Page #', 20, ' ', STR_PAD_LEFT) . "\n";

 echo str_pad('Heading 1', 30, '.');
 echo str_pad('1', 20, '.', STR_PAD_LEFT) . "\n";

 echo str_pad('Heading 2', 30, '.');
 echo str_pad('5', 20, '.', STR_PAD_LEFT) . "\n";

 echo str_pad('Heading 3', 30, '.');
 echo str_pad('11', 20, '.', STR_PAD_LEFT) . "\n";

 echo str_pad('Last Heading', 30, '.');
 echo str_pad('102', 20, '.', STR_PAD_LEFT) . "\n";
 
 echo '</pre>';

The example prints:

                 Table of Contents                 
Topic                                       Page #
Heading 1........................................1
Heading 2........................................5
Heading 3.......................................11
Last Heading...................................102

String padding using printf()

You can use the formatted string functions to pad the output by leading characters. In the following example, we use printf() function to pad the output with spaces and dots:

<?php
 echo '<pre>';
 
 printf('% 35s', "Table of Contents\n");
 printf("%-'.30s%'.20s", 'Topic',     "Page #\n");
 printf("%-'.30s%'.20s", 'Heading 1', "1\n");
 printf("%-'.30s%'.20s", 'Heading 2', "5\n");
 printf("%-'.30s%'.20s", 'Heading 3', "11\n");
 printf("%-'.30s%'.20s", 'Last Heading', "101\n"); 
 
 echo '</pre>';

The example prints:

                  Table of Contents
Topic......................................Page #
Heading 1.......................................1
Heading 2.......................................5
Heading 3......................................11
Last Heading..................................101

Manipulating Strings:

  1. Changing string case
  2. Comparing Strings
  3. Comparing Strings with Natural Order Algorithm
  4. Converting Strings into HTML
  5. Converting HTML Entities and Special Characters
  6. Formatting Strings
  7. Padding Strings
  8. Replacing Newlines with BR Tag and Trimming Whitespaces
  9. Converting character to code and code to character
  10. Repeat and Shuffle a String
  11. Count Characters and Words
  12. Manipulating substrings