Return to Portfolio

Better word_wrap Function

Script Last Updated: Monday, August 3rd, 2009
Documentation Last Updated: Monday, August 24th, 2009

The Script

Download this script

This function will wrap a string into lines of a chosen length. You can set it to wrap to the exact length by cutting off words or keep the words together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
function word_wrap($text, $line_length = 80, $new_line_character = "<br />\n", $dont_split_words = true) {
    // Holds the text lines
    $lines = array();
    // This checks if they want the lines to be the exact chosen length
    // regardless if that means having to split words.
    if($dont_split_words) {
        // Separate the text by each word to ensure they won't be split
        $words = explode(' ',$text);
        // Set the current line to 0
        $line = 0;
        // Loop through the words one by one until the second to last word
        for($i=0;$i<count($words)-1;$i++) {
            // Add words one by one to the current line 
            @$lines[$line] .= ' '.$words[$i];
            // These lines won't necessarily be exactly the chosen length, so go
            // ahead and trim the spaces at the beginning and end of the line.
            $lines[$line] = trim($lines[$line]);
            // If the next word will cause the line to exceed the chosen length,
            // then increase the line count.
            if(strlen($lines[$line]) + strlen($words[$i+1]) + 1 > $line_length) {
                $line++;
            }
        }
        // Add the last word
        @$lines[$line] .= ' ' . $words[count($words)-1];
        $lines[$line] = trim($lines[$line]);
    } else {
        // Retrieve the number of lines
        $line_count = ceil(strlen($text) / $line_length);
        // Loop through and pick out the part of the string to the chosen
        // length and build each line
        for($i=0;$i<$line_count-1;$i++) {
            $lines[] = substr($text, $i * $line_length, $line_length);
        }
        // Make the last line whose length is unknown
        $lines[] = substr($text,($line_count - 1) * $line_length);   
    }
    // Join the lines by the specified new line character
    return implode($new_line_character,$lines);   
}
?>

Usage Examples

The following is an example of how to use this function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example text string
$text = 'This function will wrap a string into lines of a chosen length. You can set it to wrap to the exact length by cutting off words or keep the words together.';

// Exact Wrap wraps lines at the exact chosen length
echo word_wrap($text, 40, '\n', false);
// Returns this
// This function will wrap a string into li
// nes of a chosen length. You can set it t
// o wrap to the exact length by cutting of
// f words or keep the words together.

// Word Wrap keeps words together
echo word_wrap($text, 40);
// Returns this
// This function will wrap a string into<br />
// lines of a chosen length. You can set it<br />
// to wrap to the exact length by cutting<br />
// off words or keep the words together.

202 Unique Visitors