PHP wordwrap() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, wordwrap() wraps a long string into lines of a chosen maximum width.

It breaks at spaces by default and can also cut very long words.

Syntax

Call wordwrap() with the values you need. Start with this line.

PHP
$wrapped = wordwrap($text, 15, PHP_EOL);
CallWhat it does
wordwrap($text, 15, PHP_EOL)Breaks the text into lines of at most 15 characters at spaces.
wordwrap("PHP is great", 6, "<br>")Uses an HTML line break. Returns "PHP is<br>great".
wordwrap("Supercalifragilistic", 8, "|", true)Cuts a long word. Returns "Supercal|ifragili|stic".

By default a word longer than the width is left whole. Set cut_long_words to true to split it.

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

Parameters

ParameterDescription
stringRequired. The text to wrap.
widthOptional. Maximum number of characters per line. Default is 75.
breakOptional. The text inserted at each line break. Default is a new line "\n".
cut_long_wordsOptional. If true, long words are cut at the width. Default is false.

Return Value

Return ValueDescription
stringThe wrapped string.

PHP Examples

Example 1 — Wrap Text at 15 Characters

Break a long sentence into shorter lines.

PHP
<?php
$text = "The quick brown fox sat over the lazy dog";

echo wordwrap($text, 15, PHP_EOL), PHP_EOL;

How It Works

  1. Set the width. Each line can hold up to 15 characters.
  2. Break at spaces. PHP never splits a normal word.
  3. Use PHP_EOL. It is the break inserted between lines.
  4. Print. Three tidy lines appear.

Example 2 — Cut Very Long Words

A word longer than the width stays whole unless you turn on cutting.

PHP
<?php
$word = "Supercalifragilistic";

echo wordwrap($word, 8, "|"), PHP_EOL;
echo wordwrap($word, 8, "|", true), PHP_EOL;

How It Works

  1. No cutting. The word is 20 letters, but there are no spaces to break at.
  2. Original kept. The first line is the same as the input.
  3. Enable cutting. The fourth argument true forces a split every 8 characters.
  4. Custom break. We used "|" so the breaks are visible.

Example 3 — Wrap for HTML

Use an HTML tag as the break string for web pages.

PHP
<?php
$text = "PHP is great for beginners";

echo wordwrap($text, 10, "<br>"), PHP_EOL;

How It Works

  1. Choose the break. "<br>" is an HTML line break.
  2. Wrap at 10. "PHP is" fits, but adding "great" would pass 10.
  3. Continue. "great for" fits on the next line.
  4. Last word. "beginners" sits alone on the final line.

Frequently Asked Questions

wordwrap() splits a long string into lines no longer than a given width, breaking at spaces.
string wordwrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false). Only the string is required.
Pass "<br>" as the break argument, or use nl2br(wordwrap($text)) to convert new lines into HTML breaks.
By default wordwrap() never splits a word. Set the fourth argument cut_long_words to true to allow it.
It counts bytes, so accented or emoji characters may be counted wrong or cut. Use mb_ functions or a custom approach for Unicode.

Back to String Functions

Browse all PHP string function tutorials.

String functions →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful