String Functions
Advertisements

PHP wordwrap() Function

Topic: PHP String ReferencePrev|Next

Description

The wordwrap() function wraps a string into new lines when it reaches the specified length.

This function wraps the string using a string break character such as <br> or \n.

The following table summarizes the technical details of this function.

Return Value: Returns the given string wrapped at the specified length.
Version: PHP 4.0.2+

Syntax

The basic syntax of the wordwrap() function is given with:

wordwrap(string, width, break, cut);

The following example shows the wordwrap() function in action.

<?php
// Sample string
$str = "The quick brown fox jumped over the lazy dog.";

// Wrapping the string
echo wordwrap($str, 15, "<br>\n");
?>

Parameters

The wordwrap() function accepts the following parameters.

Parameter Description
string Required. Specifies the string to be wrapped.
width Optional. Specifies the number of characters at which the string will be wrapped. Default is 75.
break Optional. Specifies the characters to use as break. Default is \n.
cut

Optional. Specifies whether the words longer than the specified width should be wrapped or not. This parameter can take one of the following values:

  • true – words larger than the given width will be wrapped.
  • false – words will not be wrapped. This is default.

More Examples

Here're some more examples showing how wordwrap() function actually works:

In the following example the long word "incomprehensibilities" will be broken apart or wrapped.

<?php
// Sample string
$str = "The word incomprehensibilities is very long.";

// Wrapping the string
echo wordwrap($str, 8, "<br>\n", true);
?>

In the following example the long word "incomprehensibilities" will not be broken.

<?php
// Sample string
$str = "The word incomprehensibilities is very long.";

// Wrapping the string
echo wordwrap($str, 8, "<br>\n");
?>
Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties