Home > Backend Development > PHP Tutorial > =====初学者提问:php 读取文本文件的有关问题=====

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

WBOY
Release: 2016-06-13 13:47:52
Original
820 people have browsed it

=====菜鸟提问: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;
}

这个应该可以的.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template