PHP 8 reference · String Functions
PHP wordwrap() Function
Wrap a string at a given line width by inserting line breaks between words, optionally cutting long words.
PHP 8 Since 4.0
Syntax
wordwrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
width optional |
The maximum line width in bytes. Default: 75 |
break optional |
The line break to insert. Default: "\n" |
cut_long_words optional |
true splits words longer than width. Default: false |
Return value
The wrapped string.
Examples
<?php
echo wordwrap("The quick brown fox jumps over the lazy dog", 15), "\n";
Output
The quick brown fox jumps over the lazy dog
<?php
echo wordwrap("Supercalifragilistic", 8, "\n", true), "\n";
echo wordwrap("short words here", 6, "<br>"), "\n";
Output
Supercal ifragili stic short<br>words<br>here
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
nl2br()· Insert an HTML <br> tag before every new line in a string, so plain text keeps its line breaks on a web page.str_split()· Split a string into an array of chunks of a given length, one byte each by default. Use mb_str_split() for UTF-8.- PHP Strings · tutorial