Latest web development tutorials

PHP str_pad () function

PHP String Reference PHP String Reference

Examples

Filling the right side of the string to 20 characters in the new length:

<?php
$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 可选。规定填充字符串的哪边。

可能的值:

  • STR_PAD_BOTH - 填充字符串的两侧。如果不是偶数,则右侧获得额外的填充。
  • STR_PAD_LEFT - 填充字符串的左侧。
  • STR_PAD_RIGHT - 填充字符串的右侧。这是默认的。

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);
?>

Running instance »

Example 2

Filling on both sides of the string:

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

Running instance »


PHP String Reference PHP String Reference