Thursday, September 4, 2025
HomeLanguagesPHP wordwrap() Function

PHP wordwrap() Function

The wordwrap() function is a built-in function in PHP. This function wraps a given string to a given number of characters using a string break character.

Syntax:

string wordwrap ($str, $width, $break, $cut )

Parameters : The function accepts 4 parameters as shown in the above syntax and are described below:

  1. $str: This parameter specifies the input string which is needed to break up into lines.
  2. $width: This parameter specifies the number of characters at which the string will be wrapped. That is number of characters after which the string will break.
  3. $break: This is an optional parameter and if specified appends the value at the point of breaking the string.
  4. $cut: It is a boolean parameter, if this parameter is set to TRUE, then the string is always wrapped at or before the specified width. That is it will also break a word from between if it comes in middle of the constraint specified by the parameter $width. When this parameter is set to FALSE the function does not split the word even if the width is smaller than the word width.

Return Value: The function returns a string wrapped upto specified length i.e. the string broken into lines on success, or FALSE on failure.

Below programs illustrate the wordwrap() function in PHP :

Program 1 :




<?php
  
// Input string
$str = "keep practicing at neveropen";
  
// prints the wrapped string
echo wordwrap($str, 15, "\n", TRUE); 
  
?>


Output:

keep practicing
at
neveropen

Program 2 :




<?php
  
// Input String
$text = "Be a part of neveropen.";
  
// Wrapped string
$newtext = wordwrap($text, 8, "\n", TRUE);
  
echo "$newtext\n";
  
?>


Output:

Be a
part of
neveropenfor
neveropen.

Reference:
http://php.net/manual/en/function.wordwrap.php

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11860 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS