首页  >  文章  >  php教程  >  fgetcsv函数不能读取csv文件中文字符串的解决方法

fgetcsv函数不能读取csv文件中文字符串的解决方法

WBOY
WBOY原创
2016-06-13 09:37:27920浏览

读取数据函数:

    <span function</span> getData(<span $file</span><span ) {
        </span><span $arr</span> = <span array</span><span ();
        </span><span if</span>((<span $handle</span> = <span fopen</span>(<span $file</span>,"r")) !== <span FALSE</span><span ) {
            </span><span while</span>((<span $data</span> = <span fgetcsv</span>(<span $handle</span>)) !== <span FALSE</span><span ) {
                
                </span><span $tmp</span> = <span array</span><span ();
                </span><span foreach</span>(<span $data</span> <span as</span> <span $key</span>=><span $v</span><span ) {
                    </span><span $tmp</span>[] = mb_convert_encoding(<span $v</span>,"UTF-8","gbk");  <span /*</span><span 要将gbk码转为utf-8,否则会出现乱码</span><span */</span><span 
                }
                
                </span><span $arr</span>[] = <span $tmp</span><span ;
            }
        }
        
        </span><span return</span> <span $arr</span><span ;
    }</span>

发现读取的中文字符串为空....

解决方法:将fgetcsv函数换成自定义的_fgetcsv函数

    <span function</span> _fgetcsv(&<span $handle</span>, <span $length</span> = <span null</span>, <span $d</span> = ',', <span $e</span> = '"'<span ) {
         </span><span $d</span> = <span preg_quote</span>(<span $d</span><span );
         </span><span $e</span> = <span preg_quote</span>(<span $e</span><span );
         </span><span $_line</span> = ""<span ;
         </span><span $eof</span>=<span false</span><span ;
         </span><span while</span> (<span $eof</span> != <span true</span><span ) {
             </span><span $_line</span> .= (<span empty</span> (<span $length</span>) ? <span fgets</span>(<span $handle</span>) : <span fgets</span>(<span $handle</span>, <span $length</span><span ));
             </span><span $itemcnt</span> = <span preg_match_all</span>('/' . <span $e</span> . '/', <span $_line</span>, <span $dummy</span><span );
             </span><span if</span> (<span $itemcnt</span> % 2 == 0<span )
                 </span><span $eof</span> = <span true</span><span ;
         }
         </span><span $_csv_line</span> = <span preg_replace</span>('/(?: |[ ])?$/', <span $d</span>, <span trim</span>(<span $_line</span><span ));
         </span><span $_csv_pattern</span> = '/(' . <span $e</span> . '[^' . <span $e</span> . ']*(?:' . <span $e</span> . <span $e</span> . '[^' . <span $e</span> . ']*)*' . <span $e</span> . '|[^' . <span $d</span> . ']*)' . <span $d</span> . '/'<span ;
         </span><span preg_match_all</span>(<span $_csv_pattern</span>, <span $_csv_line</span>, <span $_csv_matches</span><span );
         </span><span $_csv_data</span> = <span $_csv_matches</span>[1<span ];
         </span><span for</span> (<span $_csv_i</span> = 0; <span $_csv_i</span> < <span count</span>(<span $_csv_data</span>); <span $_csv_i</span>++<span ) {
             </span><span $_csv_data</span>[<span $_csv_i</span>] = <span preg_replace</span>('/^' . <span $e</span> . '(.*)' . <span $e</span> . '$/s', '$1' , <span $_csv_data</span>[<span $_csv_i</span><span ]);
             </span><span $_csv_data</span>[<span $_csv_i</span>] = <span str_replace</span>(<span $e</span> . <span $e</span>, <span $e</span>, <span $_csv_data</span>[<span $_csv_i</span><span ]);
         }
         </span><span return</span> <span empty</span> (<span $_line</span>) ? <span false</span> : <span $_csv_data</span><span ;
    }</span>

参考:

http://bbs.csdn.net/topics/390040230

http://hi.baidu.com/maojianlw/item/d3ee390e820cfbcb75cd3c5c

http://phpexcel.codeplex.com/(操作excel的库:phpexcel)

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn