Wrapping Text with PHP's wordwrap() Function

Learn how to use PHP's wordwrap() function to wrap lines of text. Lots of examples included.

Occasionally your PHP script will need to break up a long line of text into several shorter lines while preserving whole words. This can happen if you're formatting some text for printing or emailing, or you want to display preformatted text in a Web page using the pre element.

PHP gives you a handy function, wordwrap(), to do this job for you. This tutorial shows you how wordwrap() works.

Basic wordwrap() syntax

In its most basic form, you just pass a string to wordwrap(), and it returns the wrapped string:


$myString = "Mr. Bennet was so odd a mixture of quick parts, sarcastic humour, reserve, and caprice, that the experience of three-and-twenty years had been insufficient to make his wife understand his character.";
echo "<pre>" . wordwrap( $myString ) . "</pre>";

The above code displays:


Mr. Bennet was so odd a mixture of quick parts, sarcastic humour, reserve,
and caprice, that the experience of three-and-twenty years had been
insufficient to make his wife understand his character.

By default, wordwrap() uses a column width of 75 characters. This means that no line will be more than 75 characters long.

Specifying the column width

To specify a column width other than 75 characters, pass the width as the second argument to wordwrap():


$myString = "Mr. Bennet was so odd a mixture of quick parts, sarcastic humour, reserve, and caprice, that the experience of three-and-twenty years had been insufficient to make his wife understand his character.";
echo "<pre>" . wordwrap( $myString, 40 ) . "</pre>";

Mr. Bennet was so odd a mixture of quick
parts, sarcastic humour, reserve, and
caprice, that the experience of
three-and-twenty years had been
insufficient to make his wife understand
his character.

Using different line break characters

Normally, wordwrap() breaks text into lines by inserting a newline ("\n") character at the end of each line. However, you can specify your own line break character (or string of characters) as the third argument to wordwrap().

For example, if you're outputting the result to a Web page, and you're not using the pre element to display preformatted text, then you can break the lines using the HTML line break element, <br />:


$myString = "Mr. Bennet was so odd a mixture of quick parts, sarcastic humour, reserve, and caprice, that the experience of three-and-twenty years had been insufficient to make his wife understand his character.";
echo wordwrap( $myString, 40, "<br />" );

Wrapping very long words

What happens if the text contains a word that is longer than the desired column width? By default, this results in a line that is longer than the column width:


myString = "This text contains a very very loooooooooooooooooooong word.";
echo "<pre>" . wordwrap( $myString, 10 ) . "</pre>";

This text
contains a
very very
loooooooooooooooooooong
word.

To prevent this happening, pass true as the fourth argument to wordwrap(). This breaks any words that are longer than the specified column width:


myString = "This text contains a very very loooooooooooooooooooong word.";
echo "<pre>" . wordwrap( $myString, 10, "\n", true ) . "</pre>";

This text
contains a
very very
looooooooo
oooooooooo
ong word.

In this article you learned how to use PHP's wordwrap() function to split strings of text over several lines. You saw how to specify the maximum line length, how to use different characters to break up the lines of text, and how to deal with very long words.

Happy coding!

Learn PHP With Ease!

Written by Matt Doyle — ELATED's resident Web programming expert — Beginning PHP 5.3 is a complete introduction to PHP, covering everything in these tutorials and lots more besides. Find out how to:

  • Set up PHP on your computer
  • Use strings, arrays, functions and objects
  • Create interactive Web forms
  • Handle cookies and sessions
  • Work with files on the server
  • Build database-driven sites with MySQL
  • Send emails from your scripts
  • Create images on the fly with PHP
  • Work with regular expressions
  • Write robust, secure PHP applications

...and lots more!

“What a pleasure it's been spending hours and hours studying PHP with this magical book.” — Lulio, Florida
“The book is not only great for learning, but I find myself using it constantly as a reference as well!” — David A. Stoltz

Buy Beginning PHP 5.3 now from Amazon.comBeginning PHP 5.3 or Amazon.co.ukBeginning PHP 5.3.

Follow Elated

Related articles

Responses to this article

There are no responses yet.

Post a response

Want to add a comment, or ask a question about this article? Post a response.

To post responses you need to be a member. Not a member yet? Signing up is free, easy and only takes a minute. Sign up now.

Top of Page