wordwrap(): Wrap the string to given chars

$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
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer