wordwrap(): Wrap the string to given chars

Wrap long text

wordwrap() inserts a chosen break string at a specified width. Set the optional cut flag only when long words may be broken.

$text = 'PHP string functions are useful for text processing.';
echo wordwrap($text, 18, "\n");

Start with this current pattern. The examples below are retained, corrected where necessary, and expanded for additional variations and output.

$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
Syntax
wordwrap ( $input_string, int $width, string $break, bool $cut)
ParameterDESCRIPTION
$input_stringRequired : Input string
$widthOptional : ( default : 75 ) The number of chars after which the string will be wrapped.
$breakOptional : ( default '\n' ) To be used as line break.
$cutOptional : 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 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.
String Functions ucfirst(): To Change first char only to upper case of a string. how to change all the letters to upper case


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer