wordwrap() Function in PHP 8.2, PHP 8.3 & PHP 8.4

The wordwrap() function in PHP wraps a string to a given number of characters using a string break character. Here’s how it works in PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 and PHP 8.4.

Syntax

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

Parameters

$string – The input string
$width – The number of characters at which the string will be wrapped (default: 75)
$break – The line is broken using this character (default: “\n”)
$cut_long_words – If true, words longer than $width will be cut (default: false)

Return Value

Returns the wrapped string.

Examples

<?php
$text = "The quick brown fox jumps over the lazy dog.";
echo wordwrap($text, 15, "\n");
?>

Output

The quick brown
fox jumps over
the lazy dog.

With Cut Enabled

<?php
$text = "Supercalifragilisticexpialidocious";
echo wordwrap($text, 10, "\n", true);
?>

Output

Supercalif

ragilistic
expialidoc
ious

Changes in PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 and PHP 8.4

There have been no significant changes to the wordwrap() function in PHP 8, PHP 8.1, PHP 8.2, PHP 8.3 and PHP 8.4. The function behaves the same way as in previous PHP versions.