wordwrap ( $input_string, int $width, string $break, bool $cut)
Parameter
DESCRIPTION
$input_string
Required : Input string
$width
Optional : ( default : 75 ) The number of chars after which the string will be wrapped.
$break
Optional : ( default '\n' ) To be used as line break.
$cut
Optional : Boolean ( True or False ). TRUE : String is wrapped at or before the $width FALSE :( default ) String is wrapped after the word ( no split of word )
Output is wrapped string
Let us try some examples
$str="Welcome to Plus2net.com PHP Section to learn about PHP built-in string functions";
echo wordwrap($str);
Output is here
Welcome to Plus2net.com PHP Section to learn about PHP built-in string functions
There is a default line break ( '\n') after string and before function words. This is not visible so we will modify the script by using nl2br() like this.
$str="Welcome to Plus2net.com PHP Section to learn about PHP built-in string functions";
echo nl2br(wordwrap($str));
Output is here
Welcome to Plus2net.com PHP Section to learn about PHP built-in string
functions
Using parameters $width and $break
Above code ads one default line break ('\n') after default width ( 75 char ). Now we will add <BR> as line break with our own $width
$str="Welcome to Plus2net.com PHP Section to learn about PHP built-in string functions";
echo wordwrap($str,20,'<br>');
Output is here
Welcome to
Plus2net.com PHP
Section to learn
about PHP built-in
string functions
Using parameters $cut
In above output there is no split of word at the end of the line. This is because of default value of $cut as FALSE. Check the code now.
$str="Welcome to Plus2net.com abcdefghijklmnopqrstuvwxyz";
echo wordwrap($str,20,'<BR>');
Output is here
Welcome to
Plus2net.com
abcdefghijklmnopqrstuvwxyz
Now let us make $cut = True
$str="Welcome to Plus2net.com abcdefghijklmnopqrstuvwxyz";
echo wordwrap($str,20,'<BR>',true);
Welcome to
Plus2net.com
abcdefghijklmnopqrst
uvwxyz
The long word ( more than 20 chars ) at the end is broken into a new line.