Maison >développement back-end >tutoriel php >fgetcsv函数不能读取csv文件中文字符串的解决方法_PHP教程

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

WBOY
WBOYoriginal
2016-07-13 10:32:141205parcourir

读取数据函数:

    <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...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn