php fgetcsv - 字符集编码问题 [英] php fgetcsv - charset encoding problems

查看:38
本文介绍了php fgetcsv - 字符集编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 PHP 5.3 fgetcsv 函数时,由于编码问题,我遇到了一些问题.请注意,该文件具有西班牙语特殊"拉丁字符,如图形重音 á、é、í ï 等...

Using PHP 5.3 fgetcsv function, I am experiencing some problems due to encoding matters. Note that that file has spanish "special" latin characters like graphic accents á, é, í ï, etc...

我得到的 CSV 文件导出了我在 MS 2008 for Mac Excel 文件中的一些结构化数据.

I get the CSV file exporting some structured data I have in an MS 2008 for Mac Excel file.

如果我用 Mac OS X TextEdit 应用程序打开它,一切似乎都很完美.

If I open it with Mac OS X TextEdit application, everything seems to go perfect.

但是当我开始使用 PHP 程序并尝试使用 fgetcsv PHP 函数读取 CSV 时,我无法正确读取字符集.

But when I get down to my PHP program and try to read the CSV using that fgetcsv PHP function, I am not getting it to read properly the charset.

/**
 * @Route("/cvsLoad", name="_csv_load")
 * @Template()
 */
public function cvsLoadAction(){
    //setlocale(LC_ALL, 'es_ES.UTF-8');
    $reader = new Reader($this->get('kernel')->getRootDir().'/../web/uploads/documents/question_images/2/41/masiva.csv');

    $i = 1;
    $r = array("hhh" => $reader -> getAll());

    return new Response(json_encode($r, 200));
}

如您所见,我还尝试使用 setlocalees_ES.UTF-8.但没有什么可以让它发挥作用.

As you can see, I have tried also to use a setlocale to es_ES.UTF-8. But nothing get it working.

阅读部分来了:

public function getRow()
{
    if (($row = fgetcsv($this->_handle, 10000, $this->_delimiter)) !== false) {
        $this->_line++;
        return $this->_headers ? array_combine($this->_headers, $row) : $row;
    } else {
        return false;
    }
}

看看每行读取后我在 $row 变量中得到了什么:

See what I get in the $row variable after each row reading:

那些 ? 字符应该是带有图形重音的元音.

Those ? characters are supposed to be vowels with graphic accents on them.

那边有什么线索吗?如果我使用 MS Excel for Windows 会起作用吗?如何在运行时知道文件的确切编码并在读取之前进行设置?

Any clue over there? Would it work if I used MS Excel for Windows? How can I know in run time the exact encoding of the file and set it before reading it?

(对于那些讲西班牙语的人,不要被那些文本中如此糟糕的医学内容吓到;)).

(For those spanish speakers, don't get frightened with such awful medical stuff in those texts ;)).

推荐答案

试试这个:

function convert( $str ) {
    return iconv( "Windows-1252", "UTF-8", $str );
}

public function getRow()
{
    if (($row = fgetcsv($this->_handle, 10000, $this->_delimiter)) !== false) {
        $row = array_map( "convert", $row );
        $this->_line++;
        return $this->_headers ? array_combine($this->_headers, $row) : $row;
    } else {
        return false;
    }
}

这篇关于php fgetcsv - 字符集编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆