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

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

2 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

2 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

2 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

2 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

2 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

2 months ago