3

I have found a similar thread already where I got:

$sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string);

This doesn't seem to work in my function though:

<?function first_sentence($content) {
    $content = html_entity_decode(strip_tags($content));   
    $content = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $content);
    return $content;


}?>

It seems to be not taking into account the first sentence when a sentence ends as the end of a paragraph. Any ideas?

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Trinitrotoluene
  • 1,388
  • 5
  • 20
  • 39
  • Can you include a link to the original thread where you got the code please? Never mind, I think I got it, [Does anyone have a PHP snippet of code for grabbing the first “sentence” in a string?](http://stackoverflow.com/questions/1135467/does-anyone-have-a-php-snippet-of-code-for-grabbing-the-first-sentence-in-a-str). –  Jul 02 '11 at 00:49
  • Have you looked at any of the other "first sentence with regex" questions, like this one, [First Sentence Regex](http://stackoverflow.com/questions/1569091/first-sentence-regex)? –  Jul 02 '11 at 00:51
  • This one looks promising /^.{150,}?[.?!]+(?=\s|$)/ but not sure how I'd incorporate it into a function – Trinitrotoluene Jul 02 '11 at 00:57
  • 1
    What do you mean when the sentence ends as the result of a paragraph? Is there still a period in this case or is it using a different delimeter? – DaOgre Jul 28 '11 at 23:17
  • What's the function of '\\1'? I'm trying to use this as well, but when there's multiple paragraphs present, I get more than 1 sentence returned – Hamman Samuel Feb 07 '14 at 12:27

3 Answers3

6
/**
 * Get the first sentence of a string.
 *
 * If no ending punctuation is found then $text will
 * be returned as the sentence. If $strict is set
 * to TRUE then FALSE will be returned instead.
 *
 * @param  string  $text   Text
 * @param  boolean $strict Sentences *must* end with one of the $end characters
 * @param  string  $end    Ending punctuation
 * @return string|bool     Sentence or FALSE if none was found
 */
function firstSentence($text, $strict = false, $end = '.?!') {
    preg_match("/^[^{$end}]+[{$end}]/", $text, $result);
    if (empty($result)) {
        return ($strict ? false : $text);
    }
    return $result[0];
}

// Returns "This is a sentence."
$one = firstSentence('This is a sentence. And another sentence.');

// Returns "This is a sentence"
$two = firstSentence('This is a sentence');

// Returns FALSE
$three = firstSentence('This is a sentence', true);
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
1

In case you need n number of sentences, you can use the following code. I adapted code from: https://stackoverflow.com/a/22337095 & https://stackoverflow.com/a/10494335

/**
 * Get n number of first sentences of a string.
 *
 * If no ending punctuation is found then $text will
 * be returned as the sentence. If $strict is set
 * to TRUE then FALSE will be returned instead.
 *
 * @param  string  $text   Text
 * int     $no_sentences   Number of sentences to extract
 * @param  boolean $strict Sentences *must* end with one of the $end characters
 * @param  string  $end    Ending punctuation
 * @return string|bool     Sentences or FALSE if none was found
 */

function firstSentences($text, $no_sentences, $strict = false , $end = '.?!;:') {

    $result = preg_split('/(?<=['.$end.'])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);

    if (empty($result)) {
        return ($strict ? false : $text);
    }

    $ouput = '';
    for ($i = 0; $i < $no_sentences; $i++) {
        $ouput .= $result[$i].' ';
    }
    return $ouput;

}
Community
  • 1
  • 1
0

Here's correct syntax, sorry for my first response:

function firstSentence($string) {
      $sentences = explode(".", $string);
      return $sentences[0];
}
Thomas Thorogood
  • 2,150
  • 3
  • 24
  • 30