ホームページ  >  記事  >  バックエンド開発  >  fgetcsv関数がcsvファイル内の中国語文字列を読み込めない問題の解決策_PHPチュートリアル

fgetcsv関数がcsvファイル内の中国語文字列を読み込めない問題の解決策_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:32:141185ブラウズ

读取数据函数:

    <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)

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/756407.htmlTechArticle读取数据函数: getData( = (( = ( ,"r")) !== (( = ( )) !== = ( = [] = mb_convert_encoding( ,"UTF-8","gbk"); [] = 发现读取的中文字符串为空.... 解决方法:将fg...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。