Given a string of sentences in Russian. Print the longest word in the sentence. If there are several such words, then print the first one. In a word, consider a character set that does not contain spaces, punctuation marks and is limited to spaces, punctuation marks, or the beginning / end of a line.

Closed due to the fact that off-topic participants Dmitriy Simushev , Ksenia , Alex , user194374, Kromster 15 Dec '16 at 15:41 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • How to solve the problem by using methods already ready for working with an array of strings in php? - Kryshtop
  • five
    This question should be closed, because according to the rules of the community, the question should not be reduced to solving student assignments. - Dmitriy Simushev

3 answers 3

The explode function will get an array of words in the sentence, and then you will calculate the length of each word, you can through strlen

  • still need to get rid of punctuation marks!) - Kryshtop
  • then I paraphrase: as from the sentence "hello, my name is Alakai!" - pull out {"hello,", "me,", "name is", "Alakai". } - Kryshtop
  • You can replace the commas with spaces first, and then as I described above) - quaresma89

For lovers of the functional approach:

 $input = 'Fooo bar bazzz, фыва-олдж.'; $longest = array_reduce(preg_split('/[^a-zа-яё]+/ui', $input), function ($longest, $curr) { return mb_strlen($longest) < mb_strlen($curr) ? $curr : $longest; }, ''); echo($longest); // "bazzz" 
     $words = explode(" ", preg_replace("/[^A-ZА-ЯЁ ]/ui", " ", $str)); $long = ""; foreach ($words as $word) $long = (mb_strlen($word) > mb_strlen($long)) ? $word : $long; echo $long; 
    • one
      The range of characters AZ (as, however, А-ЯЁ ) in the regular expression is redundant, because The case-insensitive flag is specified. - Dmitriy Simushev
    • @DmitriySimushev, thanks - check1st