zoukankan      html  css  js  c++  java
  • 自定义函数-获取文件的前几行数据

    /**
         * Method to tail (a few last rows) of a file.
         *
         * @param     $filename
         * @param int $lines
         * @param int $buffer
         *
         * @return string
         */
        public function tail($filename, $lines = 10, $buffer = 4096)
        {
            $f      = fopen($filename, 'rb');
            $output = '';
    
            fseek($f, -1, SEEK_END);
    
            if ("
    " != fread($f, 1)) {
                --$lines;
            }
    
            while (ftell($f) > 0 && $lines >= 0) {
                $seek = min(ftell($f), $buffer);
                fseek($f, -$seek, SEEK_CUR);
                $output = ($chunk = fread($f, $seek)).$output;
                fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
                $lines -= substr_count($chunk, "
    ");
            }
    
            while ($lines++ < 0) {
                $output = substr($output, strpos($output, "
    ") + 1);
            }
    
            fclose($f);
    
            return $output;
        }`
  • 相关阅读:
    Java并发编程
    Git
    Spring Boot
    IDEA工具
    Java基础
    数据库架构
    设计模式
    网络基础
    管理知识
    linux安装数据库mysql
  • 原文地址:https://www.cnblogs.com/tgzmos/p/14858545.html
Copyright © 2011-2022 走看看