Latest web development tutorials

PHP wordwrap () function

PHP String Reference PHP String Reference

Examples

According to a specified length of string off-line processing:

<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>n");
?>

Running instance »

Definition and Usage

wordwrap () function in accordance with the specified length of the string off-line processing.

Note: This function may be the beginning of the line left blank.


grammar

wordwrap( string,width,break,cut )

参数 描述
string 必需。规定要进行折行的字符串。
width 可选。规定最大行宽度。默认是 75。
break 可选。规定作为分隔符使用的字符(字串断开字符)。默认是 "\n"。
cut 可选。规定是否对大于指定宽度的单词进行折行:
  • FALSE - 默认。不折行
  • TRUE - 折行

technical details

return value: If successful, the string after the wrap is returned. If it fails, it returns FALSE.
PHP version: 4.0.2+
Update log: In PHP 4.0.3, add the cut parameters.


More examples

Example 1

Use all parameters:

<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>n",TRUE);
?>

Running instance »

Example 2

Wrap the string:

<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15);
?>

HTML output of the code above is as follows (view source):

<!DOCTYPE html>
<html>
<body>
An example of a
long word is:
Supercalifragulistic
</body>
</html>

Browser output of the code above is as follows:

An example of a long word is: Supercalifragulistic

Running instance »


PHP String Reference PHP String Reference