PHP - Padding Strings with str_pad()

Introduction

PHP str_pad() can pad string.

The function returns the string padded on the right using space characters (by default):

Demo

<?php
echo str_pad("Hello, world!", 20); // Displays "Hello, world!       "
?>

Result

To pad using characters other than space, pass a string to use as an optional third argument.

Note that this can be either a single character or a string of characters; in the latter case, the string is repeated as needed to pad out the input string:

Demo

<?php
// Displays "Hello, world!*******"
echo str_pad("Hello, world!", 20, "*") . "\n";

// Displays "Hello, world!1231231"
echo str_pad("Hello, world!", 20, "123") . "\n";
?>// w  ww. j  a v  a  2  s.  co  m

Result

You can make str_pad() add padding to the left of the string, or to both the left and the right of the string.

To do this, pass an optional fourth argument comprising one of the following built-in constants:

  • STR_PAD_RIGHT to pad the string on the right (the default setting), left-aligning the string
  • STR_PAD_LEFT to pad the string on the left, right-aligning the string
  • STR_PAD_BOTH to pad the string on both the left and the right, centering the result as much as possible

The following example adds padding to both the left and right of a string:

Demo

<?php
echo str_pad("Hello, world!", 20, "*", STR_PAD_BOTH) . "\n";
?>

Result

Related Topic