Web Design that taps into the haromny and vision of your dreams.

Get First Sentence from a String in PHP

By on in Coding

36 words, estimated reading time 1 minutes.

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;
 
}

Last updated on: Thursday 22nd June 2017

 

Comments

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.