PHP wordwrap()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Wrap the string to the specified length:

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

Run Example »

Definition and Usage

The wordwrap() function wraps a string to the specified length.

Note:This function may leave whitespace at the beginning of lines.


Syntax

wordwrap(string,width,break,cut)

Parameter Description
string Required. Specifies the string to be wrapped.
width Optional. Specifies the maximum line width. Default is 75.
break Optional. Specifies the character to be used as a separator (string break character). Default is "\n".
cut Optional. Specifies whether to wrap words larger than the specified width:
  • FALSE - Default. Do not wrap.
  • TRUE - Wrap.

Technical Details

Return Value: Returns the wrapped string on success, or FALSE on failure.
PHP Version: 4.0.2+
Changelog: In PHP 4.0.3, thecutparameter was added.


More Examples

Example 1

Use all parameters:

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

Run Example »

Example 2

Wrap a string:

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

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

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

The browser output of the above code is as follows:

An example of a long word is: Supercalifragulistic

Run Example »


PHP String 参考手册PHP String Reference Manual Other Extensions