PHP Calculating Average Word Length – String Function

<?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);
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.