=====菜鸟提问: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;
}
这个应该可以的.
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号