PHP wordwrap() function

Example

Perform word wrapping on a string at specified lengths:

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

Run Instances

Definition and Usage

The wordwrap() function performs word wrapping on a string at specified lengths.

Comments:This function may leave whitespace characters at the beginning of the line.

Syntax

wordwrap(string,width,break,cut)
Parameter Description
string Required. Specify the string to be wrapped.
width Optional. Specify the maximum line width. The default is 75.
break Optional. Specify the character (or string) used as a separator. The default is "\n".
cut

Optional. Specify whether to wrap words longer than the specified width:

  • FALSE - Default. No-wrap
  • TRUE - Word wrap

Technical Details

Return Value: If successful, it returns the wrapped string. If not, it returns FALSE.
PHP Version: 4.0.2+
Update Log: In PHP 4.0.3, a new feature was added 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);
?>

Run Instances

Example 2

Word wrap strings:

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

HTML output of the above code (please view the source code):

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

Browser output of the above code:

An example of a long word: Supercalifragulistic

Run Instances