PHP str_pad()関数
例
文字列の右側に、新しい長さ20文字までパッドします。
<?php
$str = "Hello World";
echo str_pad($str,20,".");
?>
定義と使用法
str_pad()関数は、文字列を新しい長さにパディングします。
構文
str_pad(string,length,pad_string,pad_type)
パラメータ値
Parameter | Description |
---|---|
string | Required. Specifies the string to pad |
length | Required. Specifies the new string length. If this value is less than the original length of the string, nothing will be done |
pad_string | Optional. Specifies the string to use for padding. Default is whitespace |
pad_type | Optional. Specifies what side to pad the string. Possible values:
|
技術的な詳細
戻り値: | 埋め込まれた文字列を返します |
---|---|
PHPバージョン: | 4.0.1+ |
その他の例
例
弦の左側にパッドを入れます。
<?php
$str = "Hello World";
echo str_pad($str,20,".",STR_PAD_LEFT);
?>
例
弦の両側にパッドを入れます。
<?php
$str = "Hello World";
echo str_pad($str,20,".:",STR_PAD_BOTH);
?>
❮PHP文字列リファレンス