PHP - get first two sentences of a text?

后端 未结 9 1881
慢半拍i
慢半拍i 2021-02-05 22:52

My variable $content contains my text. I want to create an excerpt from $content and display the first sentence and if the sentence is shorter than 15

相关标签:
9条回答
  • 2021-02-05 23:20

    I figured it out and it was pretty simple though:

    <?php
        $content = "My name is Luka. I live on the second floor. I live upstairs from you. Yes I think you've seen me before. ";
        $dot = ".";
    
        $position = stripos ($content, $dot); //find first dot position
    
        if($position) { //if there's a dot in our soruce text do
            $offset = $position + 1; //prepare offset
            $position2 = stripos ($content, $dot, $offset); //find second dot using offset
            $first_two = substr($content, 0, $position2); //put two first sentences under $first_two
    
            echo $first_two . '.'; //add a dot
        }
    
        else {  //if there are no dots
            //do nothing
        }
    ?>
    
    0 讨论(0)
  • 2021-02-05 23:20

    I know this is an old post but I was looking for the same thing.

    preg_match('/^([^.!?]*[\.!?]+){0,2}/', strip_tags($text), $abstract);
    echo $abstract[0];
    
    0 讨论(0)
  • 2021-02-05 23:22

    I wrote a function to do something similar to this on one of our websites. I'm sure it could be tweaked to get your exact result out of it.

    Basically, you give it a string of text and the amount of words you want to have it trim to. It will then trim to that amount of words. If the last word it finds doesn't end the sentence, it will continue over the amount of words you specified until it reaches the end of the sentence. Hope it helps!

    //This function intelligently trims a body of text to a certain
    //number of words, but will not break a sentence.
    function smart_trim($string, $truncation) {
        $matches = preg_split("/\s+/", $string);
        $count = count($matches);
    
        if($count > $truncation) {
            //Grab the last word; we need to determine if
            //it is the end of the sentence or not
            $last_word = strip_tags($matches[$truncation-1]);
            $lw_count = strlen($last_word);
    
            //The last word in our truncation has a sentence ender
            if($last_word[$lw_count-1] == "." || $last_word[$lw_count-1] == "?" || $last_word[$lw_count-1] == "!") {
                for($i=$truncation;$i<$count;$i++) {
                    unset($matches[$i]);
                }
    
            //The last word in our truncation doesn't have a sentence ender, find the next one
            } else {
                //Check each word following the last word until
                //we determine a sentence's ending
                for($i=($truncation);$i<$count;$i++) {
                    if($ending_found != TRUE) {
                        $len = strlen(strip_tags($matches[$i]));
                        if($matches[$i][$len-1] == "." || $matches[$i][$len-1] == "?" || $matches[$i][$len-1] == "!") {
                            //Test to see if the next word starts with a capital
                            if($matches[$i+1][0] == strtoupper($matches[$i+1][0])) {
                                $ending_found = TRUE;
                            }
                        }
                    } else {
                        unset($matches[$i]);
                    }
                }
            }
    
            //Check to make sure we still have a closing <p> tag at the end
            $body = implode(' ', $matches);
            if(substr($body, -4) != "</p>") {
                $body = $body."</p>";
            }
    
            return $body; 
        } else {
            return $string;
        }
    }
    
    0 讨论(0)
  • 2021-02-05 23:23

    There is one for words - wordwrap

    Example Code:

    <?php
    
    for ($i = 10; $i < 26; $i++) {
        $wrappedtext = wordwrap("Lorem ipsum dolor sit amet", $i, "\n");
        echo substr($wrappedtext, 0, strpos($wrappedtext, "\n")) . "\n";
    }
    

    Output:

    Lorem
    Lorem ipsum
    Lorem ipsum
    Lorem ipsum
    Lorem ipsum
    Lorem ipsum
    Lorem ipsum
    Lorem ipsum dolor
    Lorem ipsum dolor
    Lorem ipsum dolor
    Lorem ipsum dolor
    Lorem ipsum dolor sit
    Lorem ipsum dolor sit
    Lorem ipsum dolor sit
    Lorem ipsum dolor sit
    Lorem ipsum dolor sit
    
    0 讨论(0)
  • 2021-02-05 23:28

    This would make sure it never returned a half-word;

    $short = substr($content, 0, 100);
    $short = explode(' ', $short);
    array_pop($short);
    $short = implode(' ', $short);
    print $short;
    
    0 讨论(0)
  • 2021-02-05 23:30

    Here's a quick helper method that I wrote to get the first N sentences of a given body of text. It takes periods, question marks, and exclamation points into account and defaults to 2 sentences.

    function tease($body, $sentencesToDisplay = 2) {
        $nakedBody = preg_replace('/\s+/',' ',strip_tags($body));
        $sentences = preg_split('/(\.|\?|\!)(\s)/',$nakedBody);
    
        if (count($sentences) <= $sentencesToDisplay)
            return $nakedBody;
    
        $stopAt = 0;
        foreach ($sentences as $i => $sentence) {
            $stopAt += strlen($sentence);
    
            if ($i >= $sentencesToDisplay - 1)
                break;
        }
    
        $stopAt += ($sentencesToDisplay * 2);
        return trim(substr($nakedBody, 0, $stopAt));
    }
    
    0 讨论(0)
提交回复
热议问题