溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP實現文件內容的分頁讀取

發(fā)布時間:2020-06-08 01:13:48 來源:網絡 閱讀:448 作者:990653058 欄目:web開發(fā)
/**
     * 文件內容分頁讀取函數
     * @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; //數據總行數的統(tǒng)計
        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問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI