EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP wordwrap()

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP wordwrap()

PHP wordwrap()

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)

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,063 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

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.

Examples to Implement PHP wordwrap() Function

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:

PHP wordwrap()1

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:

string

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:

PHP wordwrap()3

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:

PHP wordwrap()4

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:

profit loops

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 to PHP wordwrap(), Syntax, How does it work, examples with code and output. You can also go through our other related articles to learn more –

  1. PHP Call Function
  2. PHP levenshtein()
  3. PHP parse_str()
  4. PHP strtok()

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP list
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP strpos
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • PHP Object Injection
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training Course Learn More