The wordwrap() function is used to split a string into multiple lines. It uses a given integer value to decide where in the string to add a line break, it will add a line break every
n
characters where
n
is the given integer. You can also specify the characters to use as a line break, for example you could use either "<br>", "\n" or both depending on what the string will be used for.
Using the wordwrap() function to wrap a string into multiple lines
The wordwrap() function takes 4 arguments, although only one is required:
-
string
(required) : The string you wish to wrap into multiple lines.
-
width
(optional) : The number of characters that are allowed on each line, by default this is set to 75 characters. If the maximum amount of characters occurs within a word, that word will go to the next line.
-
break
(optional) : The characters to use as a line break, by default this is set to "\n".
-
cut
(optional) : A Boolean value that specifies whether or not the function should break words that are longer than the given width, by default this is set to false.
This function returns the wrapped string.
As an
example
, if you had the following string:
"this is a test string this is a test string this is a test string this is a test string" (length: 87 characters)
and used it in the wordwrap() function using only the required "string" argument:
$testString = "this is a test string this is a test string this is a test string this is a test string";
echo( wordwrap( $testString ) );
The output would be:
this is a test string this is a test string this is a test string this is a
test string
Using the wordwrap() function with different arguments (examples)
In the above example we only used the "string" argument and made use of the default values for the optional arguments. Here are some examples of using the 3 optional arguments:
Changing the default width from 75 to 15:
Using the string:
"this is a test string this is a test string"
$testString = "this is a test string this is a test string";
echo( wordwrap( $testString, 15 ) );
This will output the following:
this is a test
string this is
a test string
Using different characters (<br> instead of \n) for line breaks:
Using the same string as above:
$testString = "this is a test string this is a test string";
echo( wordwrap( $testString, 15, "<br>" ) );
This will output the following:
this is a test
string this is
a test string
This may appear identical to the example before, but if you use the default characters "\n" in an HTML document, the line breaks will not display and the string will appear as one line. For multipurpose use, you can use both "<br>" and "\n" as the line break characters like so:
$testString = "this is a test string this is a test string";
echo( wordwrap( $testString, 15, "<br>\n" ) );
Changing whether or not wordwrap() breaks words longer than the character maximum:
If you have an individual word taking up an entire line, the "cut" argument specifies whether or not to wrap it.
Setting "cut" as false:
$testString = "thisisaverylongword";
echo( wordwrap( $testString, 15, "\n", false ) );
Output:
thisisaverylongword
Setting "cut" as true:
$testString = "thisisaverylongword";
echo( wordwrap( $testString, 15, "\n", true ) );
Output:
thisisaverylong
word