Page 135 - Beginning PHP 5.3
P. 135
Chapter 5: Strings
$myString = “But think not that this famous town has only harpooneers,
cannibals, and bumpkins to show her visitors. Not at all. Still New Bedford
is a queer place. Had it not been for us whalemen, that tract of land would
this day perhaps have been in as howling condition as the coast of
Labrador.”;
echo “ < pre > ”;
echo wordwrap( $myString );
echo “ < /pre > ”;
This code displays the following output:
But think not that this famous town has only harpooneers, cannibals, and
bumpkins to show her visitors. Not at all. Still New Bedford is a queer
place. Had it not been for us whalemen, that tract of land would this day
perhaps have been in as howling condition as the coast of Labrador.
By default, wordwrap() makes sure each line is no longer than 75 characters, but you can change this by
passing an optional second argument:
$myString = “But think not that this famous town has only harpooneers,
cannibals, and bumpkins to show her visitors. Not at all. Still New Bedford
is a queer place. Had it not been for us whalemen, that tract of land would
this day perhaps have been in as howling condition as the coast of
Labrador.”;
echo “ < pre > ”;
echo wordwrap ( $myString, 40 );
echo “ < /pre > ”;
Here ’ s the result:
But think not that this famous town has
only harpooneers, cannibals, and
bumpkins to show her visitors. Not at
all. Still New Bedford is a queer place.
Had it not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.
If you ’ d rather split lines using a different character or characters than the newline character, pass the
character(s) you ’ d like to use as an optional third argument. For example, by splitting the lines with the
HTML line break element < br / > , the example script no longer needs to enclose the output in
< pre > ... < /pre > tags:
$myString = “But think not that this famous town has only harpooneers,
cannibals, and bumpkins to show her visitors. Not at all. Still New Bedford
is a queer place. Had it not been for us whalemen, that tract of land would
this day perhaps have been in as howling condition as the coast of
Labrador.”;
echo wordwrap ( $myString, 40, “ < br / > ” );
97
9/21/09 8:53:48 AM
c05.indd 97
c05.indd 97 9/21/09 8:53:48 AM