Learning never exhausts the mind
Home >  Coding > PHP > Get First Sentence from a String in PHP

Last Updated on by

This quick PHP snippet will return the first sentence of a specified string. Very useful for generating post or page abstracts, extracts or descriptions.
function firstPunctuation($string)
{
  $excl = strpos($string,"!");
  $stop = strpos($string,".");
  
  if ($excl === false) $excl = 9999;
  if ($stop === false) $stop = 9999;
  
  if ($excl < $stop) return $excl + 1;
  if ($excl > $stop) return $stop + 1;

}

Leave a Reply

Fields marked with * are mandatory.

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.