栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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;

    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/230156.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号