PHP String wordwrap() Function



The PHP String wordwrap() function is a built-in method. It is used to wrap a string into a specified amount of characters with the help of a string break character. Strings will break at spaces (U+0020) unless cut_long_words is set to true.

Syntax

Below is the syntax of the PHP String wordwrap() function −

string wordwrap(
   string $string,
   int $width,
   string $break,
   bool $cut_words
)

Parameters

Here are the parameters of the wordwrap() function −

  • $string − (Required) It defines the input string that needs to be separated into lines.

  • $width − (Optional) It defines the number of characters at which the string will be wrapped. That is the amount of characters after which the string breaks.

  • $break − (Optional) If specified, appends the value at the point where the string breaks.

  • $cut_words − (Optional) It is a boolean parameter; if set to TRUE, the string will always be wrapped at or before the given width. So it will break a word in between if it falls within the constraint set by the argument $width. When this argument is set to FALSE, the function does not split the word, even if its width is less than the word width.

Return Value

The wordwrap() function returns a string wrapped up to the provided length (i.e. the string broken into lines), or FALSE on failure.

PHP Version

First introduced in core PHP 4.0.2, the wordwrap() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP String wordwrap() function to get the information about the last transfer.

<?php
   $str = "tutorials point:simply easy learning";
   echo wordwrap($str,15,"\n",TRUE);
?>

Output

Here is the outcome of the following code −

tutorials
point:simply
easy learning

Example 2

In the below PHP code we will use the wordwrap() function and wrap the string at a custom width of 20 characters.

<?php
   // Define the text here
   $text = "This is an example of using the wordwrap() function in PHP.";
   
   // Wrap the string at 20 characters
   $result = wordwrap($text, 20); 

   // Display the string
   echo $result;
?> 

Output

This will generate the below output −

This is an example
of using the
wordwrap() function
in PHP.

Example 3

Now the below code uses the wordwrap() function for wrapping with a custom break character.

<?php
   // Define the text here
   $text = "This is an example of using the wordwrap() function in PHP.";
   
   // Wraps at 15 characters with \n as a break
   $result = wordwrap($text, 15, "\n"); 

   // Display the string
   echo $result;
?> 

Output

This will create the below output −

This is an
example of
using the
wordwrap()
function in
PHP.

Example 4

In the following example, we are using the wordwrap() function and use all the four parameters for wrapping if it exceed the given width.

<?php
   // Define the text here
   $text = "Thisisaverylongwordthatexceedsthewidthsetinthewordwrapfunction.";

   // Cuts words if they exceed 10 characters
   $result = wordwrap($text, 10, "\n", true); 

   // Display the wrapped string
   echo $result;
?> 

Output

Following is the output of the above code −

Thisisaver
ylongwordt
hatexceeds
thewidthse
tintheword
wrapfuncti
on.
php_function_reference.htm
Advertisements