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.
Syntaxwordwrap(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 ValueReturns the wrapped string.
Examples<?php$text = "The quick brown fox jumps over the lazy dog.";echo wordwrap($text, 15, "\n");?>
OutputThe quick brownfox jumps overthe lazy dog.
With Cut Enabled
<?php$text = "Supercalifragilisticexpialidocious";echo wordwrap($text, 10, "\n", true);?>
OutputSupercalifragilisticexpialidocious
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.