Latest web development tutorials
 

PHP wordwrap() Function

< PHP String Reference

Example

Wrap a string into new lines when it reaches a specific 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 into new lines when it reaches a specific length.

Note: This function may leave white spaces at the beginning of a line.


Syntax

wordwrap(string,width,break,cut)

Parameter Description
string Required. Specifies the string to break up into lines
width Optional. Specifies the maximum line width. Default is 75
break Optional. Specifies the characters to use as break. Default is "\n"
cut Optional. Specifies whether words longer than the specified width should be wrapped:
  • FALSE - Default. No-wrap
  • TRUE - Wrap

Technical Details

Return Value: Returns the string broken into lines on success, or FALSE on failure.
PHP Version: 4.0.2+
Changelog: The cut parameter was added in PHP 4.0.3

More Examples

Example 1

Using 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 into new lines:

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

The HTML output of the code above will be (View Source):

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

The browser output of the code above will be:

An example of a long word is: Supercalifragulistic
Run example »

< PHP String Reference