wordwrap – PHP String Functions

Syntax :

wordwrap ( string, width, break, cut );

Description :

wordwrap() function will Wraps a string to a given number of width using a string break character.


Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, It is the input string on which function will be performed.
width, Optional, Integer, It tells about the specific line width. Default width is 75.
break, Optional, User defined characters, It tells about the character with which string is to be splitted. Default break character is “\n”.
cut, Optional, Boolean, Specifies whether words longer than the specified width should be wrapped.If the cut is set to TRUE. String is always wrapped at or before the specified width. So if you have a word that is larger than the given “width” it is broken apart. (See second example). When FALSE the function does not split the word even if the width is smaller than the word width. TRUE – Wrap. FALSE – Default. No-wrap

[/table]


Output :

This will return a wrapped string of the specified length.


Related articles : chunk_split() , nl2br().


wordwrap() – PHP Functions Example 1 :
<?php
$strExample = "An example of a wordwrap() function";
echo wordwrap($strExample,15,"\n"); 
?>


In above example ,We have a string ‘An example of a wordwrap() function’. Now this function will wrapped a string to 15characters width of line separated by break character i.e. “\n” or you an say new line character. See below is the output of above code.

An example of a
word wrap
function

wordwrap() Using TRUE for parameter  “cut” – PHP Functions Example 2 :

If you want to break string with 10 characters width in a line and set the parameter cut = TRUE , this will break the long words to 10 characters also. See below example :

<?php
$strExample = "Keep working on learning newthings at tutorialmines";
echo wordwrap($strExample,10,"\n",True); 
?>

Output will be like below:

Keep
working on
learning
newthings
at
tutorialmi
nes


wordwrap – PHP Functions Example 3 :

If you want to break string with 10 characters width in a line and set the parameter cut = FALSE , this will not break the long words to 10 characters, while this will keep them intact. See below example :

<?php
$strExample = "Keep working on learningnewthingsat tutorialmines";
echo wordwrap($strExample,10,"\n",false); 
?>

Output will be like below:

Keep
working on
learningnewthingsat
tutorialmines

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *