PHP - get the first two sentences of text?

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

I already tried to remove the first 50 characters from the file and it works:

 <?php echo substr($content, 0, 50); ?> 

But I am not happy with the results (I do not want the words to be cut off).

Is there a PHP function receiving whole words / sentences, not just substr?

Thank you so much!

+6
substring php
source share
9 answers

I understood, and it was pretty simple:

 <?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 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 } ?> 
+11
source share

There is one for words - wordwrap

Code example:

 <?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 
+6
source share

Here is a quick helper method that I wrote to get the first sentences N given body of text. It takes into account periods, question marks, and exclamation points, and has two sentences by default.

 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)); } 
+6
source share

I wrote a function to do something like this on one of our sites. I am sure that this can be changed to get your exact result.

Basically, you give it a line of text and the number of words you want to trim. Then he will trim to so many words. If the last word that it finds does not end the sentence, it will continue over the number of words you have indicated until it reaches the end of the sentence. Hope this 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 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; } } 
+4
source share

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

 preg_match('/^([^.!?]*[\.!?]+){0,2}/', strip_tags($text), $abstract); echo $abstract[0]; 
+3
source share

This ensures that he never returns half the word;

 $short = substr($content, 0, 100); $short = explode(' ', $short); array_pop($short); $short = implode(' ', $short); print $short; 
+2
source share

For me it worked:

 $sentences = 2; echo implode('. ', array_slice(explode('.', $string), 0, $sentences)) . '.'; 
+2
source share

Here is a function modified from another that I found on the Internet; it removes any HTML code and first clears some MS funky characters; he then adds an extra ellipsis symbol to the content to show that it has been shortened. It is correctly broken into words, so you will not have seemingly random characters;

 /** * Function to ellipse-ify text to a specific length * * @param string $text The text to be ellipsified * @param int $max The maximum number of characters (to the word) that should be allowed * @param string $append The text to append to $text * @return string The shortened text * @author Brenley Dueck * @link http://www.brenelz.com/blog/2008/12/14/creating-an-ellipsis-in-php/ */ function ellipsis($text, $max=100, $append='&hellip;') { if (strlen($text) <= $max) return $text; $replacements = array( '|<br /><br />|' => ' ', '|&nbsp;|' => ' ', '|&rsquo;|' => '\'', '|&lsquo;|' => '\'', '|&ldquo;|' => '"', '|&rdquo;|' => '"', ); $patterns = array_keys($replacements); $replacements = array_values($replacements); $text = preg_replace($patterns, $replacements, $text); // convert double newlines to spaces $text = strip_tags($text); // remove any html. we *only* want text $out = substr($text, 0, $max); if (strpos($text, ' ') === false) return $out.$append; return preg_replace('/(\W)&(\W)/', '$1&amp;$2', (preg_replace('/\W+$/', ' ', preg_replace('/\w+$/', '', $out)))) . $append; } 

Input:

<p class="body">The latest grocery news is that the Kroger Co. is testing a new self-checkout technology. My question is: What&rsquo;s in it for me?</p> <p>Kroger said the system, from Fujitsu,

Output:

The latest grocery news is that the Kroger Co. is testing a new self-checkout technology. My question is: What in it for me? Kroger said the …

+1
source share

If I were you, I would choose only the first sentence .

 $t='Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vestibulum justo eu leo.'; //input text $fp=explode('. ',$t); //first phrase echo $fp[0].'.'; //note I added the final ponctuation 

That would greatly simplify a lot.

-3
source share

All Articles