Home >>PHP String Functions >PHP str_pad() Function
PHP str_pad() function is used to pad a string to a certain given length. Using this function we can pad the input string by any other string up to a certain length. It returns the given input string padded on the left, right, or both sides. If we do not pass the other string to the function then the input string will be padded by spaces.
Syntax:
str_pad($string, $length, $pad_string, $pad_type);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the string to be pad. |
length | This is a required parameter. This parameter defines the new string length. If it’s value is less than the original length of the string, nothing will be done. |
pad_string | This is an optional parameter. This parameter defines the string to use for padding. |
pad_type | This is an optional parameter. This parameter defines the side to which pad the string. Possible values:
|
Here is an example of str_pad() function in PHP:
<html> <body> <?php $str = "PHPTPOINT"; echo str_pad($str,20,"-"); ?> </body> </html>
Here is an another example of str_pad() function in PHP:
<html> <body> <?php $str = "Jerry"; echo str_pad($str,20,"_",STR_PAD_LEFT)."<br>"; echo str_pad($str,20,"_",STR_PAD_RIGHT)."<br>"; echo str_pad($str,20,"_",STR_PAD_BOTH)."<br>"; ?> </body> </html>