Calculating average word length
<?php
$word_count = $word_length = 0;
if ($fh = fopen('novel.txt','r')) {
while (! feof($fh)) {
if ($s = fgets($fh)) {
$words = preg_split('/\s+/',$s,-1,PREG_SPLIT_NO_EMPTY);
foreach ($words as $word) {
$word_count++;
$word_length += strlen($word);
}
}
}
}
print sprintf("The average word length over %d words is %.02f characters.",
$word_count,
$word_length/$word_count);
?>
Related examples in the same category