PHP wordwrap() Function

Jun 21, 2013, by admin

PHP Wordwrap() Definition 

The wordwrap() function wraps a string into new lines when it reaches a specific length.

Syntax :

Example :

Below example is wrapping the text using br at column 20 and return wrapped string.

  1. <?php
  2. $text = “Sometimes we need to wrap text to exactly accommodate a certain width of text.”;
  3. $newtext = wordwrap($text, 20, “</br>”);
  4. echo $newtext;
  5. ?>

 

Running above script will give you the following output:

Sometimes we need to
wrap text to exactly
accommodate a
certain width of
text.

 

Below example is wrapping the text using new linw at column 20 and return wrapped string.

  1. <?php
  2. $text = “Sometimes we need to wrap text to exactly accommodate a certain width of text.”;
  3. $newtext = wordwrap($text, 20, “n”);
  4. echo $newtext;
  5. ?>

 

Running above script will give you the following output:

Sometimes we need to
wrap text to exactly
accommodate a
certain width of
text.