Padding PHP Strings with str_pad()

Shows how to use PHP's str_pad() function to add padding to strings. Learn how to use custom padding characters, and how to pad a string on the right, left, or both sides.

In Trimming PHP Strings you learned how to remove unwanted whitespace characters from around a string. The opposite of trimming is padding — adding extra characters to either end (or both ends) of a string to make the string a certain length.

Padding is handy for lining up strings vertically for display, for adding leading zeroes to numbers, or for any situation where you need a string to be of a certain fixed length.

In this article you'll explore PHP's str_pad() function for padding strings.

Another way to pad strings is to use PHP's printf() and sprintf() functions.

Basic str_pad() usage

In its basic form, you pass 2 arguments to str_pad(): the string to pad, and how long you want the string to be after it's been padded. str_pad() then returns the string padded with spaces on the right-hand side. For example:

$myString = "Hello there";

// Displays "|Hello there         |"
echo '|' . str_pad( $myString, 20 ) . '|';

If you specify a final length that's shorter than the string to pad then the string is untouched:

$myString = "Hello there";

// Displays "|Hello there|"
echo '|' . str_pad( $myString, 5 ) . '|';

Padding with characters other than spaces

If you want to pad using another character, pass that character as the 3rd argument to str_pad():

$myString = "Hello there";

// Displays "|Hello there#########|"
echo '|' . str_pad( $myString, 20, '#' ) . '|';

You can even pass a string of characters to use as padding. str_pad() will repeat and/or truncate the string as necessary:

$myString = "Hello there";

// Displays "|Hello there123412341|"
echo '|' . str_pad( $myString, 20, '1234' ) . '|';

Padding on the left

To pad the string on the left instead of on the right, pass the flag STR_PAD_LEFT as the 4th argument to str_pad(). (The default value for this flag is STR_PAD_RIGHT.) For example:

$myString = "Hello there";

// Displays "|         Hello there|"
echo '|' . str_pad( $myString, 20, ' ', STR_PAD_LEFT ) . '|';

Padding on both sides

You can also pad a string on both sides by passing the flag STR_PAD_BOTH as the 4th argument. str_pad() attempts to add padding as evenly as possible to both sides of the string:

$myString = "Hello there";

// Displays "|    Hello there     |"
echo '|' . str_pad( $myString, 20, ' ', STR_PAD_BOTH ) . '|';

You've now looked at how to use PHP's str_pad() function to add padding to a string. You've seen how to use custom padding characters, and how to pad a string on the left, on the right, or on both sides.

Happy PHP coding!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page