PHP wordwrap() function is used for wrapping a string to a given number of characters. It will cut the string at the specified length and insert the string or character we have entered at that position.
Its Syntax will be :
wordwrap ( string , length , break character , cut mode );
- First parameter will be the string to be wrapped
- Second parameter specifies at which length wrap should occur.
- Third parameter specified the character or string to be inserted at the specified length.
- Fourth parameter is optional. This cut mode parameter specified the mode of cutting. When we specify this parameter as TRUE , cutting will occur at the middle of words too , it will strictly cut at the specified length. By default its value will be false , and cutting wont happen in the middle of a word.
You will get more clear picture about this wordwrap function after seeing the following sample PHP programs and its output
<?php $text="This tutorial will show about wordwrap function"; echo wordwrap($text,5," * ") ?>
Its output will be :
This * tutorial * will * show * about * wordwrap * function
Here we can see that single words are not splitted , they remains the same.
Now look at the next program with CUT mode parameter as TRUE
<?php $text="This tutorial will show about wordwrap function"; echo wordwrap($text,5," * ", TRUE) ?> Its output will be :
This * tutor * ial * will * show * about * wordw * rap * funct * ion
Here we can see that function has splitted the string strictly at the specified length and single words also splitted .