STR_PAD_BOTH文字の両側を埋める
STR_PAD_RIGHT文字の右側を埋める
STR_PAD_LEFT文字の左側を埋める

。PHPサンプル集。"> 【PHP サンプル集】文字列の左右を指定文字で埋める - PHP@Workshop

文字列の左右を指定文字で埋める

サンプルソース

<?php
// 文字列の両側を埋める
$s = str_pad("12345", 5, "*");
$s = str_pad("12345", 5, "*", STR_PAD_BOTH);
// 結果は *****12345*****

// 文字列の右側を埋める
$s = str_pad("12345", 5, "*", STR_PAD_RIGHT);
// 結果は 12345*****

// 文字列の左側を埋める
$s = str_pad("12345", 5, "*", STR_PAD_LEFT);
// 結果は *****12345
?>

解説

str_repeatで文字列の左右を指定文字で埋めます。

4つ目の引数は、文字を埋めるオプションです。省略時は、両側になります。

STR_PAD_BOTH文字の両側を埋める
STR_PAD_RIGHT文字の右側を埋める
STR_PAD_LEFT文字の左側を埋める




関連する内容



スポンサードリンク



 

Copyright (C) PHP@Workshop All rights reserved.