zoukankan      html  css  js  c++  java
  • 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 — 返回当前元素 
    ";
            $this->lineContent = fgets($this->fp);
            return rtrim($this->lineContent, "
    ");
        }
    
        public function next()
        {
            //echo "next — 向前移动到下一个元素 
    ";
            $this->lineNumber++;
        }
    
        public function key()
        {
            //echo "key — 返回当前元素的键 
    ";
            return $this->lineNumber;
        }
    
        public function valid()
        {
            //echo "valid — 检查当前位置是否有效 
    ";
            return feof($this->fp) ? false : true;
        }
    
        public function rewind()
        {
            //echo "rewind — 返回到迭代器的第一个元素 
    ";
            $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」
    ", $k, $v);
            $flag = 1;
        }
        $lineNumber = $k;
    }
    $endTime = microtime(true);
    
    if ($flag == 0) {
        echo "遍历了整个文件也没有找到
    ";
    }
    
    $time = bcsub($endTime, $startTime, 3) * 1000;
    echo sprintf("查找 %s 文件耗时:%s ms,共 %d 行
    ", $file, $time, $lineNumber);
  • 相关阅读:
    安装mongoDB时,总是报错,启动不了
    koa2路由
    异步操作async await
    nodeJs koa-generator脚手架
    nodeJs学习-19 个人博客案例-(1)数据字典
    nodeJs学习-18 mysql数据库了解
    nodeJs学习-17 博客案例
    nodeJs学习-16 数据字典示例
    前端图片压缩后,文件流上传
    Linux用户名显示-bash-4.1$快速排查
  • 原文地址:https://www.cnblogs.com/liugx/p/9415074.html
Copyright © 2011-2022 走看看