Current Location: Home> Function Categories> str_pad

str_pad

Use another string to fill a string to a length
Name:str_pad
Category:String
Programming Language:php
One-line Description:Fill the string with a new length.

Definition and usage

str_pad() function fills the string with a new length.

Example

Example 1

Fill the right side of the string to the new length of 30 characters:

 <?php
$str = "Hello World" ;
echo str_pad ( $str , 30 , "." ) ;
?>

Try it yourself

Example 2

Fill the left side of the string:

 <?php
$str = "Hello World" ;
echo str_pad ( $str , 30 , "." , STR_PAD_LEFT ) ;
?>

Try it yourself

Example 3

Fill both sides of the string:

 <?php
$str = "Hello World" ;
echo str_pad ( $str , 30 , ".:" , STR_PAD_BOTH ) ;
?>

Try it yourself

grammar

 str_pad ( string , length , pad_string , pad_type )
parameter describe
string Required. Specifies the string to be filled.
length Required. Specifies the new string length. If the value is less than the original length of the string, nothing is done.
pad_string Optional. Specifies the string for padding. The default is blank.
pad_type

Optional. Specifies which side of the string to fill.

Possible values:

  • STR_PAD_BOTH - Fill both sides of the string. If not even, the right side gets extra padding.
  • STR_PAD_LEFT - Fill the left side of the string.
  • STR_PAD_RIGHT - Fill the right side of the string. default.
Similar Functions
Popular Articles