PHP str_pad() Function
PHP str_pad() Function
The str_pad() function in PHP pads a string to the given length with another string.
Syntax
str_pad ( string $input , int $pad_length [, string $pad_string [, int $pad_type ]] )
Parameter
input(required)- This parameter signifies the input string.
pad_length(required)- This parameter specifies the length of the new string. If this value is less than the original length of the string, nothing will be done.
pad_string(optional)- This parameter specifies the string to use for padding. The default value is whitespace.
pad_type(optional)- This parameter specifies what side to pad the string.
The possible pad_type values are as follows:
- STR_PAD_BOTH - Pad to both sides of the string. If not an even number, the right side gets the extra padding
- STR_PAD_LEFT - Pad to the left side of the string
- STR_PAD_RIGHT - Pad to the right side of the string and this is the default value.
Return
This function returns the padded string.
Example 1
Output
The actual string: Hello World By using str_pad() function New String: Hello World....
Example 2
Output
The actual string: Hello World By using str_pad() function New String: ....Hello World
Example 3
Output
The actual string: Hello World
By using str_pad() function
New String: ..Hello World..
Example 4
Output
The actual string: Hello World By using str_pad() function New String: Hello World