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

Get First Sentence from a String in PHP

Grab the leading sentence from a paragraph.

Written By on in PHP

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



There are no comments for this post. Be the first!


Leave a Reply

Your email address will not be published.

If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me