温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP实现文件内容的分页读取

发布时间:2020-06-08 01:13:48 来源:网络 阅读:444 作者:990653058 栏目:web开发
/**
     * 文件内容分页读取函数
     * @param  string/Array $file_path_arr 文件名
     * @param  int $start 分页起始行数
     * @return json
     * @author gongzheng <990653058@qq.com>
     * @since 2014年2月10日 15:23:51
     */
function filePage( $file_path_arr, $start, $limit = 10 ) {
        $data = array( ); //返回数据格式
        $line = -1; //数据总行数的统计
        foreach ( $file_path_arr as $key => $file_path ) {
            $file_handle = new SplFileObject( $file_path );
            $file_line = -1;
            while ( !$file_handle->eof() ) {
                $line++;
                $file_line++;
                if ( $line == $start ) {
                    for ( $i = 0; $i < $limit; $i++ ) {
                        if ( !$file_handle->eof() ) {
                            $file_handle->seek( $file_line );
                            $line_arr = str_getcsv( $file_handle->current() );
                            if ( isset( $line_arr[2] ) ) {
                                $data[] = $line_arr;
                            }
                        }
                        $file_line++;
                    }
                    $current_key = $key + 1;
                    //如果小于$limit接着读取下一个文件
                    if ( isset( $file_path_arr[$current_key] ) ) {
                        while ( count( $data ) < $limit ) {
                            if ( !isset( $file_path_arr[$current_key] ) ) {
                                break;
                            }
                            $file_handle = new SplFileObject( $file_path_arr[$current_key] );
                            $num = $limit - (count( $data ));
                            for ( $i = 0; $i < $num; $i++ ) {
                                if ( !$file_handle->eof() ) {
                                    $file_handle->seek( $i );
                                    $line_arr = str_getcsv( $file_handle->current() );
                                    if ( isset( $line_arr[2] ) ) {
                                        $data[] = $line_arr;
                                    }
                                }
                            }
                            $current_key++;
                        }
                    }
                    break;
                }
            }
            break;
        }
        return $data;
    }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI