首页 > 其他 > 详细

自定义函数

时间:2021-06-07 16:12:26      阅读:8      评论:0      收藏:0      [点我收藏+]

1. 获取一个log文件的前多少行:

    /**
     * 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 ("\n" != 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, "\n");
        }

        while ($lines++ < 0) {
            $output = substr($output, strpos($output, "\n") + 1);
        }

        fclose($f);

        return $output;
    }

 

自定义函数

原文:https://www.cnblogs.com/tgzmos/p/14858545.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!