PHP wordwrap() Function
Example
Wrap a string to a given number of characters:
<?php $str = "An example of a long word is: Supercalifragulistic"; echo wordwrap($str,15,"<br>n"); ?>
Definition and Usage
The wordwrap() function wraps a string to a given number of characters.
Note: This function may leave spaces at the start of the line.
Syntax
wordwrap(string, width, break, cut)
| Parameter | Description |
|---|---|
| string | Required. The string that needs to be wrapped. |
| width | Optional. The maximum line width. Default is 75. |
| break | Optional. The character used as the line break (word break character). Default is "n". |
| cut | Optional. Whether to cut words that are longer than the specified width: * FALSE - Default. Do not cut words * TRUE - Cut words |
Technical Details
| Return Value: | Returns the wrapped string if successful, FALSE if it fails. |
| 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); ?>
Example 2
Wrapping a string:
<?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
YouTip