php中fgetcsv函数在php5.2.8 中的数据不完整
问题解析出来的数据不完整,有为空的字段,网上查了下说是在php5.2.8 中存在bug,解决办法是使用自定义函数,代码如下:
function __fgetcsv(& $handle , $length = null, $d = ',' , $e = '"' ) { $d = preg_quote( $d ); $e = preg_quote( $e ); $_line = "" ; $eof =false; while ( $eof != true) { $_line .= ( empty empty ( $length ) ? fgets ( $handle ) : fgets ( $handle , $length )); $itemcnt = preg_match_all( '/' . $e . '/' , $_line , $dummy ); if ( $itemcnt % 2 == 0) $eof = true; } $_csv_line = preg_replace( '/(?: |[ ])?$/' , $d , trim( $_line )); $_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/' ; preg_match_all( $_csv_pattern , $_csv_line , $_csv_matches ); $_csv_data = $_csv_matches [1]; for ( $_csv_i = 0; $_csv_i < count ( $_csv_data ); $_csv_i ++) { $_csv_data [ $_csv_i ] = preg_replace( '/^' . $e . '(.*)' . $e . '$/s' , '$1' , $_csv_data [ $_csv_i ]); $_csv_data [ $_csv_i ] = str_replace ( $e . $e , $e , $_csv_data [ $_csv_i ]); } return empty empty ( $_line ) ? false : $_csv_data ; }查看更多关于php中fgetcsv函数在php5.2.8 中的数据不完整 - php函数的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did30726