PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
select version() as Version;
Copy Clear
Copy Format Clear
<?php function str_average($text) { // Removing "not word" characters in $text $special = [',', ';', '-']; $text = str_replace($special, ' ', $text); $tokens = '.?'; $chunk = strtok(trim($text), $tokens); // Handle empty $text if (!is_string($chunk)) { return 0; } $sentences = []; do { $sentences[] = $chunk; } while ($chunk = strtok($tokens)); $countWords = function (int $carry, string $item) { return $carry + count(array_filter(explode(' ', $item))); }; $totalWords = array_reduce($sentences, $countWords, 0); return $totalWords / count($sentences); } echo str_average("Hello world? this is a test..."), PHP_EOL; ?>
Show:  
Copy Clear