PHP str_pad() can pad string.
The function returns the string padded on the right using space characters (by default):
<?php echo str_pad("Hello, world!", 20); // Displays "Hello, world! " ?>
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:
<?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
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:
The following example adds padding to both the left and right of a string:
<?php echo str_pad("Hello, world!", 20, "*", STR_PAD_BOTH) . "\n"; ?>