PHP str_pad () function
Examples
Filling the right side of the string to 20 characters in the new length:
<?php
$str = "Hello World";
echo str_pad($str,20,".");
?>
$str = "Hello World";
echo str_pad($str,20,".");
?>
Running instance »
Definition and Usage
str_pad () function to populate the new string length.
grammar
str_pad( string,length,pad_string,pad_type )
参数 | 描述 |
---|---|
string | 必需。规定要填充的字符串。 |
length | 必需。规定新字符串的长度。如果该值小于原始字符串的长度,则不进行任何操作。 |
pad_string | 可选。规定供填充使用的字符串。默认是空白。 |
pad_type | 可选。规定填充字符串的哪边。 可能的值:
|
technical details
return value: | Returns a string filled. |
---|---|
PHP version: | 4.0.1+ |
More examples
Example 1
Fill the left side of the string:
<?php
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>
Running instance »
Example 2
Filling on both sides of the string:
<?php
$str = "Hello World";
echo str_pad($str,20,".:",STR_PAD_BOTH);
?>
$str = "Hello World";
echo str_pad($str,20,".:",STR_PAD_BOTH);
?>
Running instance »
PHP String Reference