-4

I have dynamic data that is pulled from the database.

Something like the following

$aychFour="From the start, community-minded business. Throughout our 
25-year history in ABC, we have continually ways to give back. 
We with local charities, involvement in numerous activities & 
seasonal, as well as hosting in-store events so our guests can also.";

Since I am not sure about the contents of $aychFour, is it possible to find second period(.) in the string and truncate anything after that?

user580950
  • 3,558
  • 12
  • 49
  • 94
  • 6
    Yes, that is possible. What have you tried? What did your own research into this turn up? – KIKO Software Feb 23 '22 at 21:15
  • 1
    Your title does not match your question. Normal sentences can have, at least, 3 endings, in English: a period, a question mark, and an exclamation mark. Do you want to take these into account? – KIKO Software Feb 23 '22 at 21:18
  • And even that isn't reliable; sentences can have a `.` in the middle of them, or fragments, like `'English is a weird language!', he exclaimed.` in the case of narration, etc. At a basic level, splitting on these characters is ok, but there are reasons natural language detection and deconstruction is so difficult. – Tim Lewis Feb 23 '22 at 21:37
  • Don't forget ellipses!... – Stevish Feb 23 '22 at 21:39
  • Does this answer your question? [PHP - get first two sentences of a text?](https://stackoverflow.com/questions/4692047/php-get-first-two-sentences-of-a-text) – Don't Panic Feb 23 '22 at 21:53

1 Answers1

1

The explode() function can be usefull. You can divide the string in a array with $res=explode(".",$aychFour) and rebuild the string from the array given.

if(count($res)>1){
   $aychFour=$res[0].".".$res[1].".";
}

Another approach is to search the second dot with strpos (or a regular expression).

$pos = strpos($aychFour, '.', strpos($aychFour, '.') + 1);
if($pos > 1){
  $aychFour=substr($aychFour,0,$pos+1);
}