PHP,使用fgets跳过文本文件的第一行和最后一行

标签 php

我正在尝试找出在使用 fgets() 阅读文本文件时如何跳过文本文件的第一行和最后一行。第一行可以用 if(!$firstLine) 解决,但我不确定如何忽略最后一行,或者我忽略第一行的解决方案是否是最佳选择。

最佳答案

fgets($file); //Ignore the first line
$line = fgets($file);
$next = fgets($file); 
while ($next !== false) { //check the line after the one you will process next.  
                //This way, when $next is false, then you still have one line left you could process, the last line.
    //Do Stuff
    $line = $next;
    $next = fgets($file);
}

关于PHP,使用fgets跳过文本文件的第一行和最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13637069/

相关文章:

php - 如何在 MySQL 数据库中查找前 5 个重复条目?

php - 如何使用 SimpleHtmlDom 在 HTML 的 head 标签之间插入链接标签

php - Zend 框架 : Plugin paths

php - 用 Volley 发送参数并获得响应

php - 我想使用 paypal 支付包裹销售,哪种 paypal 方式最好?

php - 在非对象上调用成员函数 rowCount()

php - 设置 While 循环的限制并按日期排序

php - 如何显示 MySQL 与 PHP 的多对多关系

php - 如何在 yii 中将 mysql 查询转换为 CDbCriteria?

php mysql 代码不会回显