Home > Backend Development > PHP Tutorial > Solution to the problem that fgetcsv function cannot read Chinese strings in csv files_PHP tutorial

Solution to the problem that fgetcsv function cannot read Chinese strings in csv files_PHP tutorial

WBOY
Release: 2016-07-13 10:32:14
Original
1195 people have browsed it

读取数据函数:

    <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>
Copy after login

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

解决方法:将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>
Copy after login

参考:

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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template