Wrapping words in a paragraph
To wrap words in a paragraph you make use of the wordwrap function. wordwrap accepts a string that contains the text to wrap, the desired width(optional), the string breaking method(optional), and whether to cut the last word or not(optional). The following example illustrates its use.
$para="The number in the shirt is 35";
$wrapped= wordwrap($para, 10, "<br>");
print "$wrapped";
This will output:
The number
in the
shirt is
35
In the above code $para is the text to wrap, 10 is the length of the line, and <br> is our desired line break (we can also use \n). To cut the line at a maximum desired length we would have to ad a, 1 to the wordwrap function above, see the following example.
$wrapped= wordwrap($para, 3, "<br>", 1);
This will output:
The
num
ber
in
the
shi
rt
is
35
Changing string case
There are several functions to change strings to uppercase or lowercase characters. To make the first character of a string uppercase we use ucfirst. To change all the first characters on every word we use ucwords. strtoupper changes all characters to uppercase. To change all the characters of a string to lowercase we use strtolower. The following example illustrates these functions.
|
<?php
$para="the number in the shirt is 35";
print ucfirst($para)."<br>";
print ucwords($para)."<br>";
print strtoupper($para)."<br>";
print strtolower($para)."<br>";
?> |
This will output:
The number in the shirt is 35
The Number In The Shirt Is 35
THE NUMBER IN THE SHIRT IS 35
the number in the shirt is 35