Skip to main content

Truncate long string along word boundaries

Suppose you have a articles module on you website which displays the summary of top three articles in the left column. Typically you would want to display the 10 or 15 words summary with a “Read More” link to the full article. The substr function would be an easy solution except that it does not bother about the word boundaries.


* param string $str The text string to split
* param integer $words The number of words to extract. Defaults to 15
function word_split($str, $words = 15) {
    $arr = preg_split(”/[\s]+/”,  $str, $words+1);
    $arr = array_slice($arr, 0, $words);
    return join(’ ‘, $arr);

$input = ‘Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna liqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.’;

echo word_split($input, 10);

//Will output
//Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do

preg_split to the rescue

The preg_split function is used to split a string into substrings. The boundaries along which the string is to be split, are specified using a regular expressions pattern.

preg_split function takes four parameters but only the first three are relevant to us right now.

First Parameter – Pattern
The first parameter is the regular expressions pattern along which the string is to be split. In our case, we want to split the string across word boundaries. Therefore we use a predefined character class \s which matches white space characters such as space, tab, carriage return and line feed.

Second Parameter – Input String
The second parameter is the long text string which we want to split.

Third Parameter – Limit
The third parameter specifies the number of substrings which should be returned. If you set the limit to n, preg_split will return an array of n elements. The first n-1 elements will contain the substrings. The last (n th) element will contain the rest of the string.

For example see the below code:

$str = ‘one two three four five six’;
print_r(preg_split(”/[\s]+/”, $str, 4));

This would output:

    [0] => one
    [1] => two
    [2] => three
    [3] => four five six