-1

I found very usefull the function to returne the first sentence from a paragraph.

refer: Returning first sentence from variable in PHP

I have product description in xml file and i want to get the first sentence for a title and the rest as a description.

Example:

Lorem ipsum dolor sit amet. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.

With the refer, I get for title: Lorem ipsum dolor sit amet.

I want to get for description: Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.

What is the function to remove the first sentence from a paragraph? (sentence variations end = '.?!')

Kara
  • 6,115
  • 16
  • 50
  • 57
Stathis
  • 1
  • 3

1 Answers1

1

you can use explode function

$paragraph = 'Lorem ipsum dolor sit amet. Consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam';

return explode('.', $paragraph)[1];  //here i want second index

This function return an array, So to get specific sentence specify index of sentence

  • Thanks, but i don't know to write functions, can you please help? – Stathis Dec 28 '20 at 18:09
  • I find one function but i don't know how to include more symbols than '.', my descriptions has end at '.!' – Stathis Dec 28 '20 at 18:13
  • function last_sentence($content) { $pos = strrpos($content, '.'); $pos_two = strrpos($content, '.', $pos - strlen($content) - 1); if($pos_two === false) { return $content; } else { return substr($content, $pos_two+1); } } – Stathis Dec 28 '20 at 18:13
  • https://stackoverflow.com/questions/19276055/how-to-get-the-last-sentence-from-chunk-of-text-in-php – Stathis Dec 28 '20 at 18:14
  • So if you want to split string with multi delimiter you can use https://www.php.net/manual/en/function.preg-split.php function also you can see this https://stackoverflow.com/questions/4955433/php-multiple-delimiters-in-explode – Mahmoud Mohamed Ramadan Dec 28 '20 at 19:54