Skip to content

Instantly share code, notes, and snippets.

@HoangPV
Created July 30, 2017 21:50
Show Gist options
  • Save HoangPV/99f3b7e46d823af367d7f91d123a746d to your computer and use it in GitHub Desktop.
Save HoangPV/99f3b7e46d823af367d7f91d123a746d to your computer and use it in GitHub Desktop.
return the max words of sentences, which is from a text
<?php
/**
* return the max words of sentences, which is from a text
*
* @param $s
* @return mixed
* @author Phan Vu Hoang <vu-hoang.phan@ekino.com>
*/
function solution($s)
{
$delimiters = ['.', '?', '!'];
$ready = str_replace($delimiters, $delimiters[0], $s);
$launch = explode($delimiters[0], $ready);
$arr_words_count = [];
foreach ($launch as $index => $sen) {
$ps = explode(" ", $sen);
$non_letters = [];
foreach ($ps as $p) {
if (is_numeric($p) || $p === '') {
$non_letters[] = $p;
}
}
$letters = array_diff($ps, $non_letters);
$arr_words_count[] = count($letters);
}
return max($arr_words_count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment