我想计算文本文件中的单词数。下面是我尝试过的代码。php 代码工作正常,但它也计算空白。我应该添加什么才能使代码不计算空格。我的PHP代码:
<?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);
?>
芜湖不芜