The wordwrap Function
The wordwrap
function inserts a line break
(default is \n
) after every N characters
(default is 75
).
Syntax
wordwrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false): string
Example
In this example, the function will break the string every 10
characters with a line break:
<?php
$text = "Quick brown fox jumps over lazy dog";
echo wordwrap($text, 10, "<br>\n");
?>
Code execution result:
Quick<br>
brown fox<br>
jumps over<br>
lazy dog
Example
Example with forced breaking of long words:
<?php
$text = "Supercalifragilisticexpialidocious wordwrap example";
echo wordwrap($text, 8, "<br>\n", true);
?>
Code execution result:
Supercal<br>
ifragil<br>
isticexp<br>
ialidoc<br>
ious<br>
wordwra<br>
p<br>
example
Example
Example with a long URL string:
<?php
$url = "https://www.example.com/very/long/url/path/to/some/resource";
echo wordwrap($url, 20, "<br>\n", true);
?>
Code execution result:
https://www.exampl<br>
e.com/very/long/u<br>
rl/path/to/some/<br>
resource
See Also
-
the
nl2br
function,
which inserts abr
tag after a line break character