PHP wordwrap is an inbuilt function in PHP. It wraps a string to a given number of characters using a string break character. In this article, we will discuss the PHP wordwrap Function. Also, we will discuss a few examples of using it.
string wordwrap ($string, $width, $break = ‘\n’, $cut = false )
The PHP wordwrap function expects four parameters. However, two parameters are mandatory while the other two are optional. The description of the parameters is as follows:
- $string: The input string which you need to break up in different lines. Also, this is a mandatory parameter.
- $width: The number of characters after which the string will break. Also, this is a mandatory parameter.
- $break: The function appends the $break value at the point of breaking. However, this is an optional parameter. By default, the function appends a newline character after every break.
- $cut: The fourth parameter is optional and is a boolean value. If set to true, the function will also break a word if it comes in the middle of the $width parameter. However, by default, the parameter is set to false. Therefore, the function does not break any words.
The PHP wordwrap Function returns wrapped up to the specified length. Therefore, it breaks the string into lines. However, the function returns false on failure.
Let’s discuss a few examples of using the function.
Example 1: Breaking a String Upto a Length
For example, consider a simple example of breaking a string.
<?php $string = 'The quick brown fox jumped over the lazy dog.'; echo wordwrap($string, 10); ?>
In the above example, the function breaks the word after 10 characters. Since the $cut parameter is set to false (by default), the function does not break any word even if it comes in the middle of specified characters. The output of the function will be:
OUTPUT: The quick brown fox jumped over the lazy dog.
Example 2: Appending a Value After Every Break
Also, we can append a given value after every break by passing the third parameter.
<?php $string = 'The quick brown fox jumped over the lazy dog.'; echo wordwrap($string, 10, 'APPENDEDV VALUE <br/>'); ?>
The PHP wordwrap function will append the input value after every break as follows:
OUTPUT: The quickAPPENDEDV VALUE brown foxAPPENDEDV VALUE jumpedAPPENDEDV VALUE over theAPPENDEDV VALUE lazy dog.
Example 3: Breaking a Word
Similarly, we can also break a word if it comes in the middle of the specified width by passing the fourth parameter as true.
<?php $string = 'This is a very loooooooooooonnnnnggggg word'; echo wordwrap($string, 10, '<br/>', true); echo wordwrap($string, 10, '<br/>', false); ?>
//With Break Set to True This is a very looooooooo ooonnnnngg ggg word //With Break Set to False This is a very loooooooooooonnnnnggggg word
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.