PHP wordwrap() 函式
示例
當字串達到指定長度時,將其換行
<?php
$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+ |
更新日誌 | cut 引數在 PHP 4.0.3 中被新增 |
更多示例
示例
使用所有引數
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n",TRUE);
?>
自己動手試一試 »
示例
將字串換行
<?php
$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>
上述程式碼的瀏覽器輸出是
An example of a long word is: Supercalifragulistic
自己動手試一試 »
❮ PHP 字串參考