Updated April 3, 2023
Introduction to PHP wordwrap()
The PHP wordwrap() function of the PHP Programming Language is actually an inbuilt function which helps to wrap a string into many number of characters with the help of a break character inside of the wordwrap() function. It runs only for the PHP 4.0.2 version and also for other above PHP versions in order to support the function. This PHP wordwrap() function helps in wrapping a string into many new lines when it is going to reach to its given length. This function is one the best function when handling the string function in the PHP Programming Language and in some other languages too.
Syntax
wordwrap($str1, $width1, $break1, $cut1)
Explanation:
The wordwrap() function of the PHP Programming Language usually accepts four parameters as mentioned above under the syntax section.
- $str1: The $str1 Parameter of the wordwrap() function is actually helps in specifying the input string which is actually need to breakup into many lines.
- $width1: The $width1 Parameter of the wordwrap() function is actually helps in specifying the number of characters at which point the string has to be/will be wrapped. These are the number of the characters after which the string value will break.
- $break1: The $break1 Parameter of the wordwrap() function is actually an optional parameter but if it is specified then it will append the value at the exact point of string break.
- $cut1: The $cut1 Parameter of the wordwrap() function is actually a Boolean parameter. If this $cut1 parameter value is set to TRUE value then the string is always going to be wrapped at that point or before the exact specified width. That means it is also going to break a word which are between if it comes in the middle of a constraint that is actually specifying the width1 parameter. And if this cut1 parameter is set to the FALSE value then the wordwrap() function don’t even split the word even if the width parameter has less value than the word’s width.
Return Value:
The wordwrap() function helps in returning a string which is wrapped upto the specified length. It is nothing but a string which is going to be broken into many lines on success or FALSE value on failure.
How wordwrap() Function works in PHP?
The wordwrap() function of the PHP Programming Language mainly based on the four parameters which are mentioned inside of the wordwrap() function. It works by wrapping the given string into number of characters by using the breaking character “break character” but this only works for PHP 4.0.2 and for other above PHP versions. Below are the examples to implement:
Example #1
This is the example of implementing the wordwrap() function of the PHP Programming Language. Here at first a variable “$text1” is created with the string value “My Name is Pavan Kumar Sake.”. Then again another variable “$newtext1” variable is created to store the wordwrap() function along with it’s fours parameters in it. In this wordwrap() function, $str1 parameter is $text variable’s value. Then 8 is used as width parameter, for line break one of the html tag “<br>” is used, for $cut1 parameter TRUE value is passed. Then for every 8 characters the words will be wrapped and then those will be printed. This print is done with the help of the echo function of the PHP web programming language.
Code:
<?php
$text1 = "My Name is Pavan Kumar Sake.";
$newtext1 = wordwrap($text1, 8, "<br>", TRUE);
echo "$newtext1";
?>
Output:
Example #2
This example is also one of the implementation to know how wordwrap() function is working with the help of some of the parameters as mentioned in the syntax above. At first, a variable “$stringn1” is created and stored a string sentence value. Then width1 variable is stored with “10” value, break1 variable is created with “<br>” html tag value. Then wordwrap() function is used after the echo statement to print the result of the wordwrap() function. Here some strings are created by wrapping with the width “10”.
Code:
<?php
$stringn1 = "Pavan Kumar Sake a Blogger PhysicsNerd and BusinessMan";
$width1 = 10;
$break1 = "</br>";
echo wordwrap($stringn1, $width1, $break1);
?>
Output:
Example #3
This example also similar to the above example 1. At first $stringn1, $width1, $break1 variables are created with string value, integer value , <br> tag value. Then wordwrap() function is used but $cut1 parameter is not used here. If the word’s width is larger than the specified width parameter value then the whole word will be printed by wrapping at first only if the $cut1 parameter is not used.
Code:
<?php
$stringn1 = "Congratulations! Pavan Sake kumar";
$width1 = 8;
$break1 = "</br>";
echo wordwrap($stringn1, $width1, $break1);
?>
Output:
Example #4
This program is also one of the wordwrap() function implementation by mentioning the $cut1 parameter by passing TRUE value to it. Here a variable “$stringnn1” is created with string value then $width11 variable is created with 8 as value then $break11 variable is created with <br> tag as break parameter, $cut1 variable is created with TRUE as value. Then wordwrap() function is used after the echo statement. Here cut1 parameter is used inside of the function by passing TRUE as value. Even though the string width is bigger than the width11 value then word will be broken and wrapped into single string text exactly. You can check the output below.
Code:
<?php
$stringnn1 = "Congratulations! Power Ranger of the World";
$width11 = 8;
$break11 = "</br>";
$cut11 = true;
echo wordwrap($stringnn1, $width11, $break11, $cut11);
?>
Output:
Example #5
This program is so similar to the example 4 but here $cut1 parameter value is passed with the FALSE value and stringnn1 variable is passed new string value, width parameter is mentioned with 7 as value. So the output will be different. For bigger strings or smaller strings, whole strings will be printed after some spaces like that. But most of the time the width of the strings which are wrapped will be of the length “7”.
Code:
<?php
$stringnn1 = "Be a part of Game Changer :: profitloops";
$width11 = 7;
$break11 = "</br>";
$cut11 = false;
echo wordwrap($stringnn1, $width11, $break11, $cut11);
?>
Output:
Conclusion
I hope you learnt what is the definition of PHP wordwrap() function along with its syntax with all the parameters explanation, How the wordwrap() function works in PHP Programming language along with various examples of implementing the wordwrap() function to understand the wordwrap() function better.
Recommended Articles
This is a guide to PHP wordwrap(). Here we discuss an introduction, syntax, and working of wordwrap() in PHP along with different examples and code implementation. You can also go through our other related articles to learn more –