当前位置:Gxlcms > PHP教程 > =====初学者提问:php读取文本文件的有关问题=====

=====初学者提问:php读取文本文件的有关问题=====

时间:2021-07-01 10:21:17 帮助过:29人阅读

=====菜鸟提问:php 读取文本文件的问题=====
我写了一个通过php读取文本文件的函数,统计该文本的总行数,空行不计,但我不知道如何判断某行是否为空,每次都把空行统计进去了,请各位高人指点啊,我是初学php的,函数如下,$file 为指定文本文件路径
function getFileCount($file){
$count=0;

$fd= fopen($file, "r ");
while(($line=fgets($fd,4096))!= " "){
$count++;
}
fclose($fd);
return $count;
}

------解决方案--------------------
trim($line) == ' '
------解决方案--------------------
trim($line) && $count++;
------解决方案--------------------
function getFileCount($file){
$count=0;
$lines = file($file);
foreach($lines as $line)
{
if(trim($line) != ' ') $count++;
}
return $count;
}

这个应该可以的.

人气教程排行