wordwrap()
The wordwrap() function in PHP is used to wrap a string to a specified number of characters. It inserts line breaks at the appropriate points to ensure that no
Introduction
The wordwrap() function in PHP wraps a string onto multiple lines so that no line exceeds a given width. It walks the string and inserts a line break at the last space before the limit is reached, so words stay intact. This is useful for formatting plain-text output — emails, log files, console messages, or any place where you need a fixed column width and don't have CSS to do the wrapping for you.
Syntax
wordwrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false): string| Parameter | Description |
|---|---|
$string | The input string to wrap. |
$width | The column width at which to wrap. Defaults to 75. |
$break | The string inserted at each break point. Defaults to a newline "\n". |
$cut_long_words | If true, words longer than $width are split. If false (default), long words are left intact and may overrun the width. |
The function returns a new string — it does not modify the original. Note that wordwrap() wraps on spaces, not on word boundaries in general; a long run of non-space characters is only broken when $cut_long_words is true.
Basic example
Wrap a sentence at 20 characters:
Output:
This is a long piece
of text that needs
to be wrapped.No line exceeds 20 characters. Each break replaces the space at the last word boundary that fits within the limit, so words are never split.
Wrapping for HTML with a custom break
The $break argument can be any string, not just a newline. To wrap text inside HTML, use a <br /> tag (combined with \n so the source stays readable):
<?php
$text = "The quick brown fox jumped over the lazy dog.";
echo wordwrap($text, 15, "<br />\n");Output:
The quick brown<br />
fox jumped over<br />
the lazy dog.If your goal is simply to convert existing newlines to <br> tags, reach for nl2br() instead — wordwrap() is for creating the line breaks, nl2br() is for converting them.
Cutting long words
By default a word longer than $width is left whole and overflows the line. Pass true as the fourth argument to force the function to break inside long words:
<?php
$word = "Supercalifragilisticexpialidocious";
echo "Default (no cut):\n";
echo wordwrap($word, 10, "\n"), "\n\n";
echo "With cut:\n";
echo wordwrap($word, 10, "\n", true);Output:
Default (no cut):
Supercalifragilisticexpialidocious
With cut:
Supercalif
ragilistic
expialidoc
iousUse the cut option for content that must respect a hard column limit — for example fixed-width terminal output or URLs that would otherwise blow past the margin.
Common gotchas
- Existing newlines are not normalized.
wordwrap()treats characters between break points by count; it doesn't re-flow text that already contains its own line breaks. Clean up input first if needed. - Width is in bytes, not characters. Like most of PHP's classic string functions,
wordwrap()is byte-based. Multibyte (UTF-8) characters can therefore be miscounted and may even be split mid-character when$cut_long_wordsistrue. For multibyte-safe wrapping, use a dedicated library such asmb_wordwrap()fromsymfony/polyfillor wrap on grapheme boundaries yourself. - The break string counts toward nothing. The width measures only the original text, so a long
$break(like"<br />\n") won't shorten your lines.
Related functions
nl2br()— insert HTML line breaks before existing newlines.chunk-split()— split a string into chunks of equal length regardless of word boundaries.str-split()— break a string into an array of fixed-length pieces.substr()— extract a portion of a string.explode()— split a string into an array by a delimiter.