The wordwrap Function

The wordwrap function inserts a line break (default is \n) after every N characters (default is 75).

Syntax

phpwordwrap(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<?php
  $text = "Quick brown fox jumps over lazy dog";
  echo wordwrap($text, 10, "<br>\n");
?>

Code execution result:

phpQuick<br>
brown fox<br>
jumps over<br>
lazy dog

Example

Example with forced breaking of long words:

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

Code execution result:

phpSupercal<br>
ifragil<br>
isticexp<br>
ialidoc<br>
ious<br>
wordwra<br>
p<br>
example

Example

Example with a long URL string:

php<?php
  $url = "https://www.example.com/very/long/url/path/to/some/resource";
  echo wordwrap($url, 20, "<br>\n", true);
?>

Code execution result:

phphttps://www.exampl<br>
e.com/very/long/u<br>
rl/path/to/some/<br>
resource

See Also

  • the nl2br function,
    which inserts a br tag after a line break character