-1

Tell me, please, how on PHP to get the first five sentences from the text below?

Cicero famously orated against his! Political opponent Lucius Sergius Catilina. Погода сегодня хорошая!

Occasionally the first Oration against Catiline is taken for type specimens: quo usque tandem abutere, Catilina, patientia nostra? Quam diu etiam furor iste tuus nos eludet? Cicero writing letters. Or maybe not.

KvinT
  • 37
  • 6
  • Could you please show us what you have tried? How to define a SENTENCE? Should it be end with ".", "!" or "?" ? Think again and do some experiment first. – CK Wong Apr 27 '18 at 02:08
  • I tried to pull the sentence into an array through regular expressions (with function preg_match), but I could not do it. – KvinT Apr 27 '18 at 02:16
  • `preg_match` returns true or false but it can't return any index or length. If you want to substring the sentence, you need an index and length. – CK Wong Apr 27 '18 at 02:22
  • 1
    preg_match has a third parameter, where the result is written – KvinT Apr 27 '18 at 02:24
  • >> [Possible Duplicate](https://stackoverflow.com/questions/4692047/php-get-first-two-sentences-of-a-text) – Karlo Kokkak Apr 27 '18 at 02:48
  • [For you reference](https://www.stockeasymoney.com/ns2/test/SqlTest03.php). I did an example for you. Check it if it is what you want. – CK Wong Apr 27 '18 at 02:51
  • http://php.net/manual/en/intlbreakiterator.createsentenceinstance.php – user3942918 Apr 27 '18 at 04:16

2 Answers2

0

Not sure how well this will suit your needs, but in the manual for explode there is a "multiexplode" function.

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "Cicero famously orated against his! Political opponent Lucius Sergius Catilina. Погода сегодня хорошая!

Occasionally the first Oration against Catiline is taken for type specimens: quo usque tandem abutere, Catilina, patientia nostra? Quam diu etiam furor iste tuus nos eludet? Cicero writing letters. Or maybe not.";

$exploded = multiexplode(array(".", "!", "?"),$text);

print_r(array_slice($exploded,0,5));

Then I use array_slice to get the first five items in the array.

Andreas
  • 23,610
  • 6
  • 30
  • 62
-1

It's kind of hard to parse with the non-english characters and the vague definition of a sentence, but the below code should do the tweak. Maybe with a few minor tweaks. It was originally found Here as Mr. Blue commented above. I've tested a few times and works quite well.

Function stripSentence($Text, $Number) {
        $Stripped = preg_replace('/\s+/',' ',strip_tags($Text));
        $Sentences = preg_split('/(\.|\?|\!)(\s)/',$Stripped);
        If (COUNT($Sentences) <= $Number) {
            Return $Stripped;
        } Else {
            $Stop = 0;
            ForEach($Sentences AS $i => $Sentence) {
                $Stop += StrLen($Sentence);
                If ($i >= $Number - 1) {
                    Break;
                }
            }
            $Stop += ($Number * 2);
            Return Trim(SubStr($Stripped, 0, $Stop));
        }
    }
Timothy Bomer
  • 504
  • 5
  • 21