Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Playing with strings Wish List | Report Bug | About Me ]

4.7.8     Wrapping your lines: wordwrap()

This is NOT the latest copy of this book; click here for the latest version.

string wordwrap ( string source [, int width [, string break [, boolean cut]]])

Although web pages wrap text automatically, there are two situations when you might want to wrap text yourself:

  • When printing to a console as opposed to a web page, text does not wrap automatically. Therefore, unless you want your users to scroll around a lot, it is best to wrap text for them.

  • When printing to a web page that has been designed to exactly accommodate a certain width of text, allowing browsers to wrap text whenever they want will likely lead to the design getting warped.

In either of these situations, the wordwrap() function comes to your aid. If you pass a sentence of text into wordwrap() with no other parameters, it will return that same string wrapped at the 75-character mark using "\n" for new lines. However, you can pass both the size and new line marker as parameters two and three if you want to, like this:

<?php
    $text
= "Word wrap will split this text up into smaller lines, which makes for easier reading and neater layout.";
    
$text = wordwrap($text, 20, "<BR />");
    print
$text;
?>

Running that script will give you the following output:

Word wrap will split<BR />this text up into<BR />smaller lines, which<BR />makes for easier<BR />reading and neater<BR />layout.

As you can see, wordwrap() has used <BR />, a HTML new line marker, and split up words at the 20-character mark. Note that wordwrap() always pessimistically wraps words - that is, if you set the second parameter to 20, wordwrap() will always wrap when it hits 20 characters or under - not 21, 22, etc. The only exception to this is if you have words that are individually longer than 20 characters - wordwrap() will not break up a word, and so may return larger chunks than the limit you set.

If you really want your limit to be a hard maximum, you can supply 1 as a fourth parameter, which enables "cut" mode - words over the limit will be cut up if this is enabled. Here is an example of cut mode in action:

<?php
    $text
= "Micro-organism is a very long word.";
    
$text = wordwrap($text, 6, "\n", 1);
    print
$text;
?>

That will output the following:

Micro-
organi
sm is
a very
long
word.




<< 4.7.7 Trimming whitespace: trim(), ltrim(), and rtrim()   4.7.9 Changing string case: strtoupper(), strtolower(), ucfirst(), and ucwords() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
peter monu - 13 Oct 2008

i want to sell my property
this is my output


but i want to
i want to sell ......

A PHP User - 13 Oct 2008

ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddxxxxxxxxxxxdddddiiiiiiiiiiiiiiiiiiiiii

testing wordwrap. i don't think it works :/

A PHP User - 13 Oct 2008

But this wordwrap no run good if you need XHTML + HTMLENTITIES:

Example: try a wordwrap in this string:

"TRY: ñññññññññññññññññññññññññññññññññññññññññññññññññ How run ?"

If you cut this each 20, you page no run XHTML, becouse this cut in this form:

ññ&ntild e;ñññ ;ñññ ñññ& ntilde;ññ&...

And you Page response in FF or Opera:

"
Error de parseo XML: malformado
Lugar: http://google.com/worwrap.php
Línea 18, Columna 5:&nti lde;ññ&
----^
"

Some solution ?

NOTE: we need used HTMLENTITIES !

Logically if chars is != of ñ or < or >, etc...

No problem, but... If char is a entiti HTML as [&] ?...

Oh!, you page NO run!.

Try this wordwrap each 10 this string:

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Samplely parce failed!

A PHP User - 13 Oct 2008

But this wordwrap no run good if you need XHTML + HTMLENTITIES:

Example: try a wordwrap in this string:

"TRY: ñññññññññññññññññññññññññññññññññññññññññññññññññ How run ?"

If you cut this each 20, you page no run XHTML, becouse this cut in this form:

ññ&ntild e;ñññ ;ñññ ñññ& ntilde;ññ&...

And you Page response in FF or Opera:

"
Error de parseo XML: malformado
Lugar: http://google.com/worwrap.php
Línea 18, Columna 5:&nti lde;ññ&
----^
"

Some solution ?

NOTE: we need used HTMLENTITIES !

cast3r - 13 Oct 2008

"<BR />" is an XHTML new line marker, not HTML;
one of (many) reasons why your site doesnt validate...

A PHP User - 13 Oct 2008

<?php

print `ls`


?>

Another PHP User - 13 Oct 2008

The comment below about wordwrap giving the wrong results may be in error. I tried the same thing and found that if I removed the $ from in front of wordwrap like this:

echo(wordwrap($bigString, 20, '<br />'));

it worked as advertised.

Jim Plush - 13 Oct 2008

its usually wise to add the 1 as the fourth cut parameter, this prevents people from putting in words like yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

that would break your page display because the html table will not wrap it and unless you specify the cut parameter wordwrap will not wrap it either.

A PHP User - 13 Oct 2008

I tried using wordwrap() using this snippet:


$bigString = "In either of these situations, the wordwrap() function comes to your aid. If you pass a sentence of text into wordwrap() with no other parameters, it will return that same string wrapped at the 75-character mark using \n for new lines. However, you can pass both the size and new line marker as parameters two and three if you want to";

echo $wordwrap($bigString, 20, '<br />');

output:

In either of these
situations, the
wordwrap()
function comes
to your aid. If you
pass a
sentence of text
into wordwrap() with
no other
parameters, it
will return that
same string
wrapped at the
75-character mark
using
for new lines.
However, you can
pass both the
size and new
line marker as
parameters two and
three
if you want to

------------------------
the second argument splits the $bigString into 20 lines as opposed to what you've said that it would return a string split at 20 characters mark!

-burgiz-



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow