文字列のセンタリング
Posted feedbacks - PHP
マルチバイト非対応。
ex)
echo sprintf('<pre>[%s]</pre>', Centering('test', 4));
('test', 4) => [test]
('test', 8) => [ test ]
('test', 5) => [test ]
('test', 2) => [es]
ex)
echo sprintf('<pre>[%s]</pre>', Centering('test', 4));
('test', 4) => [test]
('test', 8) => [ test ]
('test', 5) => [test ]
('test', 2) => [es]
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
function Centering($str, $width)
{
$slen = strlen($str);
if ($slen < $width) {
$str = str_pad($str, $width, ' ', STR_PAD_BOTH);
} else {
$str = substr($str, (int)(($slen - $width)/2), $width);
}
return $str;
}
?>
|


nobsun
#4089()
Rating0/2=0.00
文字列を指定のカラム幅にセンタリング配置する関数を示してください。文字列の長さが指定した幅より長い場合には文字列の両端をできるだけ均等に切り落して指定幅に収めてください。1文字は1カラムに収まるものと仮定してかまいません。
[ reply ]