index.php
<?php $wordCount = 0; $wordLength = 0; if ($fh = fopen('test.txt','r')) { while (! feof($fh)) { if ($s = fgets($fh)) { $totalWords = preg_split('/\s+/',$s,-1,PREG_SPLIT_NO_EMPTY); foreach ($totalWords as $word) { $wordCount++; $wordLength += strlen($word); } } } } print sprintf("The average word length over %d words is %.02f characters.", $wordCount, $wordLength/$wordCount); ?>