PHP wordwrap()関数
例
指定された長さに従って文字列を折り返します:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
サンプルを実行 »
定義と使用法
wordwrap() 関数は、指定された長さに従って文字列を折り返します。
注:この関数は、行の先頭にスペースを残すことがあります。
構文
wordwrap(string,width,break,cut)
| パラメータ | 説明 |
|---|---|
| string | 必須。折り返す文字列を指定します。 |
| width | 省略可能。最大行幅を指定します。デフォルトは 75 です。 |
| break | 省略可能。区切り文字として使用する文字(文字列の区切り文字)を指定します。デフォルトは "\n" です。 |
| cut | 省略可能。指定された幅よりも大きい単語を折り返すかどうかを指定します:
|
技術詳細
| 戻り値: | 成功した場合は、折り返し後の文字列を返します。失敗した場合は FALSE を返します。 |
|---|---|
| PHP バージョン: | 4.0.2+ |
| 更新ログ: | PHP 4.0.3 で、新しく追加されたcutパラメータ。 |
より多くの例
例 1
すべてのパラメータを使用します:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>n",TRUE);
?>
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>n",TRUE);
?>
サンプルを実行 »
例 2
文字列を折り返します:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15);
?>
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15);
?>
上記コードの HTML 出力は次のとおりです(ソースを表示):
<!DOCTYPE html>
<html>
<body>
An example of a
long word is:
Supercalifragulistic
</body>
</html>
<html>
<body>
An example of a
long word is:
Supercalifragulistic
</body>
</html>
上記コードのブラウザ出力は次のとおりです:
An example of a long word is: Supercalifragulistic
サンプルを実行 »
PHP String リファレンスマニュアル その他の拡張