Home > Article > Backend Development > php string filling function
Definition and usage
str_pad() function pads the string to the new length.
Syntax
str_pad(<span>string</span>,<span>length</span>,<span>pad_string</span>,<span>pad_type</span>)
Parameters | Description |
---|---|
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, 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:
|
Padding the left side of the string:
<?php $str = "Hello World"; echo str_pad($str,30,".",STR_PAD_LEFT); ?>Example 2:
Padding both sides of the string:
<?php $str = "Hello World"; echo str_pad($str,30,".:",STR_PAD_BOTH); ?>
The above introduces the PHP string filling function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.