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);

 

posted @ 2018-08-03 16:51  liugx  阅读(1126)  评论(0)    收藏  举报