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");
The examples below show additional variations, expected output and practical usage.
$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, 75, "\n", false)
| 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 ) |
$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
\n. Here we use an HTML <br> break and a width of 20.
$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
$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
With $cut set to true, a word longer than the selected width may be split.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.