PHP str_getcsv删除变音符号 [英] PHP str_getcsv removes umlauts

查看:169
本文介绍了PHP str_getcsv删除变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中解析包含german变音符号( - >ä,ö,ü,Ä,Ö,Ü)的CSV字符串时,遇到一个小问题。

I encountered a little problem when parsing CSV-Strings that contain german umlauts (-> ä, ö, ü, Ä, Ö, Ü) in PHP.

假设以下csv输入字符串:

Assume the following csv input string:

w;x;y;z
48;OSL;Oslo Stock Exchange;B
49;OTB;Österreichische Termin- und Optionenbörse;C
50;VIE;Wiener Börse;D

和适当的PHP代码用于解析字符串并创建一个数组,其中包含来自csv-String的数据:

And the appropriate PHP code used to parse the string and create an array which contains the data from the csv-String:

public static function parseCSV($csvString) {
    $rows = str_getcsv($csvString, "\n");
    // Remove headers ..
    $header = array_shift($rows);
    $cols = str_getcsv($header, ';');
    if(!$cols || count($cols)!=4) {
        return null;
    }
    // Parse rows ..
    $data = array();
    foreach($rows as $row) {
        $cols = str_getcsv($row, ';');
        $data[] = array('w'=>$cols[0], 'x'=>$cols[1], 'y'=>$cols[2], 'z'=>$cols[3]);
    }
    if(count($data)>0) {
        return $data;
    }
    return null;
}

使用给定的csv-string调用上述函数的结果为:

The result of calling the above function with the given csv-string results in:

Array
(
    [0] => Array
        (
            [w] => 48
            [x] => OSL
            [y] => Oslo Stock Exchange
            [z] => B
        )

    [1] => Array
        (
            [w] => 49
            [x] => OTB
            [y] => sterreichische Termin- und Optionenbörse
            [z] => C
        )

    [2] => Array
        (
            [w] => 50
            [x] => VIE
            [y] => Wiener Börse
            [z] => D
        )
)

请注意,第二个条目缺少Ö。
这只会发生,如果umlaut直接放在列分隔符后面。
如果多个变音符按顺序排列,也就是ÖÖÖsterreich - >sterreich。
csv-string使用HTML-Form发送,因此内容获得URL编码。
我使用Linux服务器,使用utf-8编码,csv-string在解析之前看起来正确。

Note that the second entry is missing the Ö. This only happens, if the umlaut is placed directly after the column separator character. It also happens, if more than one umlaut is places in sequence, i.e. "ÖÖÖsterreich" -> "sterreich". The csv-string is sent using a HTML-Form, thus the content gets URL-encoded. I use a Linux server, with utf-8 encoding and the csv-string looks correct before parsing.

任何想法?

推荐答案

假设fgetcsv( http://php.net/manual/en/function.fgetcsv.php )的工作方式类似于str_getcsv(),然后引用手册页:

Assuming fgetcsv (http://php.net/manual/en/function.fgetcsv.php) works similar to str_getcsv() then to quote the man page:


通过此函数将区域设置考虑在
中。如果LANG是例如
en_US.UTF-8,一个字节
编码的文件被这个
函数读取错误。

Locale setting is taken into account by this function. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.


$ b b

,那么您应该尝试使用setlocale设置语言环境
http:// php.net/manual/en/function.setlocale.php

如果这不起作用,请尝试启用多字节重载
http://www.php.net/manual/en/mbstring.overload.php a>

if this doesn't work, try enabling multi byte overload http://www.php.net/manual/en/mbstring.overload.php

甚至更好,使用标准框架库(如Zend / Symfony库)拉出数据

or even better, using a standard framework library like a Zend/Symfony library to pull the data out

这篇关于PHP str_getcsv删除变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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