Pad the right side of the string to a new length of 20 characters:
<?php$str = "Hello World";echo str_pad($str,20,".");?>The str_pad() function pads a string to its new length.
str_pad( string,length,pad_string,pad_type )
parameter | describe |
---|---|
string | Required. Specifies the string to be filled. |
length | Required. Specifies the length of the new string. If the value is less than the length of the original string, no operation is performed. |
pad_string | Optional. Specifies the string used for padding. The default is blank. |
pad_type | Optional. Specifies which side of the string to fill. Possible values: STR_PAD_BOTH - Pad both sides of a string. If it's not an even number, the right side gets extra padding. STR_PAD_LEFT - pad the left side of the string. STR_PAD_RIGHT - Pad the right side of the string. This is the default. |
Return value: | Returns the padded string. |
---|---|
PHP version: | 4.0.1+ |
Pad the left side of a string:
<?php$str = "Hello World";echo str_pad($str,20,".",STR_PAD_LEFT);?>Pad both sides of a string:
<?php$str = "Hello World";echo str_pad($str,20,".:",STR_PAD_BOTH);?>