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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

wordwrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false): string

Parameters

ParameterDescription
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

Wrap at 15 charactersTry it yourself
<?php
echo wordwrap("The quick brown fox jumps over the lazy dog", 15), "\n";

Output

The quick brown
fox jumps over
the lazy dog
Cut long words, custom breakTry it yourself
<?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

VersionChange
PHP 4.0Added

Was this page helpful?