PHP 通过实现 Iterator(迭代器)接口来读取大文件文本

读了NGINX的access日志,bnb_manage_access.log(31M) 和  bnb_wechat_access.log(50M)

 

 

 

 

 附上代码:

<?php
/**
 * User: szliugx@gmail.com
 * Date: 2018/8/3
 * Time: 下午3:34
 */

class FileIterator implements Iterator
{
    // 打开的文件句柄
    private $fp;

    // 打开的文件行数
    private $lineNumber;

    // 行内容
    private $lineContent;

    public function __construct($file)
    {
        $fp = fopen($file, "r");
        if (!$fp) {
            throw new Exception("「{$file}」不能打开");
        }
        $this->fp = $fp;
    }

    public function current()
    {
        //echo "current — 返回当前元素 \n";
        $this->lineContent = fgets($this->fp);
        return rtrim($this->lineContent, "\n");
    }

    public function next()
    {
        //echo "next — 向前移动到下一个元素 \n";
        $this->lineNumber++;
    }

    public function key()
    {
        //echo "key — 返回当前元素的键 \n";
        return $this->lineNumber;
    }

    public function valid()
    {
        //echo "valid — 检查当前位置是否有效 \n";
        return feof($this->fp) ? false : true;
    }

    public function rewind()
    {
        //echo "rewind — 返回到迭代器的第一个元素 \n";
        $this->lineNumber = 1;
    }


}

$file = "/Users/liugx/work/php/laravel_wechat_demo/bnb_wechat_access.log";

try {
    $fileIterator = new FileIterator($file);
} catch (Exception $e) {
    echo "出错啦:" . $e->getMessage();
    exit;
}

$flag = 0;
$lineNumber = 0;
$startTime = microtime(true);
foreach ($fileIterator as $k => $v) {
    if (strpos($v, "测试查找") !== false) {
        echo sprintf("找到啦:%d行出现了内容「%s」\n", $k, $v);
        $flag = 1;
    }
    $lineNumber = $k;
}
$endTime = microtime(true);

if ($flag == 0) {
    echo "遍历了整个文件也没有找到\n";
}

$time = bcsub($endTime, $startTime, 3) * 1000;
echo sprintf("查找 %s 文件耗时:%s ms,共 %d 行\n", $file, $time, $lineNumber);

 

转载于:https://www.cnblogs.com/liugx/p/9415074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值