Categories: PHP

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
jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

11 months ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

12 months ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago