Make a string so many characters long with this function. If your original string is already that length or longer, nothing will happen.

The length you specify is the total length you want the string to be, not how many characters to add.

By default it will apply spaces to the right of your string but you can specify a different string to be used instead of spaces, as well as which side (or both) is to be padded. It will use whatever is needed of the pad string - one character at a time. If this pad string is longer than is required to do the job, the “extra” characters simply won’t be used.

$str = "monkey";
$a = str_pad($str, 17) . "|";                // monkey           |
$b = str_pad($str, 17, "x");                 // monkeyxxxxxxxxxxx
$b = str_pad($str, 17, "x", STR_PAD_RIGHT);  // monkeyxxxxxxxxxxx
$c = str_pad($str, 17, "x", STR_PAD_LEFT);   // xxxxxxxxxxxmonkey
$d = str_pad($str, 17, "x", STR_PAD_BOTH);   // xxxxxmonkeyxxxxxx
 
$e = str_pad($str, 17, "ABC");               // monkeyABCABCABCAB
$f = str_pad($str, 17, "ABC", STR_PAD_BOTH); // ABCABmonkeyABCABC
 
$g = str_pad($str, 5);                       // monkey