title: PHP to count words from a txt file
body: <p>i want to count the number of words in a text file. below is my code i have tried. The php code works fine but it is also counting the white spaces. what should i add in order for the code to not count the white-spaces.
  My php code:</p>
  
  <pre><code>&lt;?php 
  $count = 0;  
  
  //Opens a file in read mode  
  $file = fopen("trial.txt", "r");  
  
  //Gets each line till end of file is reached  
  while (($line = fgets($file)) !== false) {  
      //Splits each line into words  
      $words = explode(" ", $line);  
      //Counts each word  
      $count = $count + count($words);  
  }  
  
  print("Number of words : " . $count);  
  fclose($file);
  ?&gt; 
  </code></pre>