Getting An Excerpt Of Text With PHP

I have talked before about truncating strings to include whole words, but what if you want to have complete sentences? The following function will truncate a string the number of words specified and until the sentence ends.

function excerpt($paragraph, $limit)
{
 $tok = strtok($paragraph, " ");
 while ($tok !== false) {
  $text .= " ".$tok;
  $words++;
  if (($words >= $limit) && ((substr($tok, -1) == "!") || (substr($tok, -1) == ".") || (substr($tok, -1) == "?"))) {
   break;
  }
  $tok = strtok(" ");
 }
 return ltrim($text);
}

Using this function you can create an excerpt of any text you want, as long as it has a sentence in it. Take the following section of text.

$string = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sollicitudin. Suspendisse potenti. Mauris lorem elit, sodales at, aliquet eu, porta vitae, velit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus rhoncus ipsum non urna. Donec et quam. Sed vel lectus in enim porttitor vulputate. Vestibulum at nisi a nulla vulputate mattis. Integer luctus mauris id dolor. In hac habitasse platea dictumst. Aliquam placerat consectetuer ipsum.  Aliquam erat volutpat. Fusce ac nisi a neque laoreet feugiat. Fusce euismod ligula sit amet enim. Sed quis leo. Aenean vel nisi in nunc accumsan accumsan. Nullam egestas orci ut justo. Pellentesque suscipit. Suspendisse pretium fermentum nunc. Mauris ac orci vel nibh facilisis sollicitudin. Pellentesque mattis metus eu mi. Curabitur pharetra ante nec enim. Vestibulum pede.  Maecenas sodales tincidunt erat. Mauris tincidunt. Mauris justo sem, malesuada quis, ultricies non, malesuada nec, lorem. Vivamus laoreet lacus id lorem. Nulla rutrum nisi et urna. Praesent nec turpis ac orci feugiat egestas. Nunc tellus justo, pretium ac, molestie a, luctus a, lacus. Nunc pellentesque iaculis dui. Morbi vel sapien id ligula laoreet euismod. Sed convallis est eu mi. Nam suscipit, eros eu imperdiet malesuada, odio velit iaculis ipsum, eu accumsan ipsum metus id ipsum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer urna. In orci est, semper vitae, adipiscing sed, tempor vel, massa. Sed venenatis, augue eu ornare ullamcorper, nulla enim mollis neque, nec luctus nunc risus non risus. Curabitur mattis arcu mollis magna. Maecenas sagittis metus a dolor. Aliquam accumsan lacinia sem. In hac habitasse platea dictumst.';

This can be passed through the function to get the first 50 words and the rest of the ending sentence like this.

echo excerpt($string, 50);

Produces the following output.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sollicitudin. Suspendisse potenti. Mauris lorem elit, sodales at, aliquet eu, porta vitae, velit. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus rhoncus ipsum non urna. Donec et quam. Sed vel lectus in enim porttitor vulputate. Vestibulum at nisi a nulla vulputate mattis.

Comments

Does this work with UTF-8 (ie multiple byte strings)?
Permalink
I can only say maybe. The PHP manual is a bit sketchy on this and states that:
A string is series of characters. Before PHP 6, a character is the same as a byte. That is, there are exactly 256 different characters possible. This also implies that PHP has no native support of Unicode.
So in order to use multiple byte strings you might need to use the PHP utf8_decode() function to convert the string into a usable format before passing it through this function. The opposite function is utf8_encode(). The function hinges around the use of the PHP strtok() function so as long as that receives a string that PHP can understand then it should be fine.
Name
Philip Norton
Permalink
Nice function, thx - saved me some work. But one has to use while($tok !== false) or any single 0 will terminate the function (e.g. "aaa 0 bbb ccc." will always be "aaa").
Permalink
Good point! I have updated the script accordingly.
Name
Philip Norton
Permalink
Also thank you for saving me some work. I used your function to create a helper for CakePHP. I will write an article about it and send you the link.
Permalink
I just realized CakePHP already has a function for this so I won't be creating one after all.
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.