Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

wordwrap

Wraps a long string at word boundaries.

Syntax

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

Parameters

ParameterTypeDescription
$stringstringThe source string.
$widthintOptional. The maximum line length.
$breakstringOptional. What to insert at the break.
$cut_long_wordsboolOptional. `true` breaks words longer than the width.

Return value

stringThe wrapped string.

Examples

Lines no longer than 15 characters:

<?php
 
echo wordwrap('The quick brown fox jumps over the lazy dog', 15);

Output:

The quick brown
fox jumps over
the lazy dog

Notes

The function counts bytes, so for non-ASCII text the width comes out at half of what you expect.

See also

  • str_split — Splits a string into pieces of a given length.
  • nl2br — Inserts a br tag before every line break.

In the tutorial

  • Strings — Working with strings in PHP — quotes, concatenation, length, searching, replacing and multibyte functions.

Thanks for reading!

Donate to the site — we work hard for you.