Latest web development tutorials

PHP wordwrap() 函數

PHP String 參考手冊 PHP String參考手冊

實例

按照指定長度對字符串進行折行處理:

<?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 - 默认。不折行
  • TRUE - 折行

技術細節

返回值: 如果成功,則返回折行後的字符串。 如果失敗,則返回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);
?>

運行實例»

實例2

對字符串進行折行:

<?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 String 參考手冊 PHP String參考手冊