PHP Tutorial - PHP wordwrap() Function






Definition

wordwrap() function wrap lines against a length. with no other parameters wordwrap() wraps string at the 75-character mark using "\n" for new lines.

Syntax

PHP wordwrap() Function has the following syntax.

string wordwrap ( string str [, int line_length [, string break_char [, bool cut]]] )

Parameter

We can pass both the size and new line marker as the second and third parameters.

Return

Returns the given string wrapped at the specified length.

Example

<?PHP
$text = "This a long test from java2s.com. This a long test from java2s.com. This a long test from java2s.com. "; 
$text = wordwrap($text, 20, "<br />"); 
print $text; 
?>

Supply 1 as the fourth parameter enables "cut" mode: words over the limit will be cut up.

Here is an example of cut mode in action:

<?PHP
$text = "This a long test from java2s.com. This a long test from java2s.com. This a long test from java2s.com."; 
$text = wordwrap($text, 6, "\n", 1); 
print $text;  
?>