PHP wordwrap()

About me: I am an everyday Joe Schmoe that enjoys spending way too much time on the internet. Internet technologies such as XHTML, PHP, CSS, and FLASH are my best friends and hobbies. They are daily learning experiences and I enjoy sharing what I learn with others.

PHP wordwrap functionLet’s discuss the PHP built-in function called wordwrap(). Wordwrap is a very basic function that is built into PHP. Web page borders and divs may automatically wrap text on your screen, but there are a few situations when you might want to wrap text yourself be it on the screen, a console, or printing. When printing to a console, text will not wrap automatically, it is best to wrap text for them using a function like wordwrap(). Printing text to a web page that has been designed to accommodate a certain width of text can be better controlled if utilizing this function as well as the div width tag in your CSS.

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 a 75 character default. However, you can pass both the size as parameter number two if you want, like this:

The basic syntax for the wordwrap() function would be something like:
$s =”your text”;
print wordwrap($s); //default being 75 characters.

NOTE: You can use concatenation (.) and add a bit of formatting. Inside the parameters for your function simply use a comma and then the length. It will look something like:

<?php
$s = "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.";
print "\n".wordwrap($s,30)."\n";
print "\n".wordwrap($s,60)."\n";
?>

Running that script will give you the following output:
Wrapped to 30 characters-

Four score and seven years ago
our fathers brought forth on
this continent a new nation,
conceived in liberty and
dedicated to the proposition
that all men are created
equal.

Wrapped to 60 characters-

Four score and seven years ago our fathers brought forth on
this continent a new nation, conceived in liberty and
dedicated to the proposition that all men are created equal.

You can see that it breaks down the lines at the 30 and 60 character lengths. The only exception to this is if you have words that are individually longer than 30 or 60 characters – wordwrap() will not break up a word, and so may return larger sentences than the limit set. What if we want a hard count set at 30 and 60 characters? If you really want your limit to be a hard count and or maximum limit, you can simply supply the fourth parameter in the function (set to true,1) – words over the limit will be wrapped.
Like thus:
print wordwrap($s, 30, “\n”, 1);
You can see there are a variety of ways to write and implement the wordwrap() function. Hopefully some portion of this has been helpful.

Happy Coding!

Z

Tags: ,

Register!


That's it - back to the top of page!

Recent Posts

POPULAR POSTS!