SimpleXML和中文 [英] SimpleXML and Chinese

查看:150
本文介绍了SimpleXML和中文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于处理以下RSS Feed:,在获取数据后使用以下代码:



$ response = simplexml_load_string($ data);



但是,当我询问简单的xml对象时,99%的汉字和字符串会消失。



尝试将以下内容转换为utf8:



$ data = iconv(UTF-8,UTF-





p>之前的数据hits simplexml_load_string其100%罚款。

解决方案

什么意思?你描述的声音就像一个编码问题。



当您从RSS服务器请求数据时,您将会看到一个链接,如果它在处理的一部分被破坏,获取特定字符编码中的数据。您应该找到的第一件事是该数据的编码。

 数据网址:http://tw.blog.search .yahoo.com / rss ei = UTF-8& p =%E6%95%B8%E4%BD%8D%E6%99%82%E4%BB%A3%20%E9%9B%9C%E8% AA%8C& pvid = QAEnPXeg.ioIuO7iSzUg9wQIc1LBPk3uWh8ABnsa 

根据网站标题, UTF-8 。这是标准的XML编码。



然而,如果数据不是UTF-8编码的,而标题说的话,你需要找出正确的数据编码



接下来要检查的是如果 simplexml_load_string()能够处理UTF-8数据。



我不使用simplexml,我使用DomDocument。所以我不能说如果不。不过,我建议您改用 DomDocument 。它绝对支持UTF-8加载,并且返回的所有数据也是以UTF-8编码的。你应该安全地假设simplexml正确处理UTF-8。



链接的下一部分是你的显示。你写你的数据坏了。你怎么这么说?如何查询simplexml对象?






重新访问编码链



如所写,编码就像一个链。如果一个元件断开,则整体结果被损坏。要找出它在哪里断开,每个元素都必须自己检查。




  • 输入数据:所有检查都正确:

    • 检查:编码数据是否似乎是UTF-8?结果:是的。从给定的数据URL获得的输入数据,验证UTF-8编码。

    • 检查:原始xml数据是否将其标记为UTF-8编码?结果:是的。这可以在第一个字节内验证:<?xml version =1.0encoding =UTF-8?>


  • 简单XML数据

    • 检查:simple_xml是否支持UTF- 8编码?结果:是。

    • 检查:simple_xml是否以UTF-8编码返回值?结果:是和否。通常simple_xml支持包含UTF-8编码的文本的属性,但是带有xml数据的simple_xml对象实例的 var_dump()表明它不支持CDATA。 CDATA用于所讨论的数据。



    此时,这看起来像是错误正面临。但是,您可以将所有CDATA元素转换为文本。为此,您需要在加载XML数据时指定一个选项。该选项是一个常量,名为 LIBXML_NOCDATA ,它将合并CDATA作为文本节点。



    以下是我用于上述测试的示例代码,演示如何使用选项: / p>

      $ data_url ='http://tw.blog.search.yahoo.com/rss?ei=UTF-8&p =%E6%95%B8%E4%BD%8D%E6%99%82%E4%BB%A3%20%E9%9B%9C%E8%AA%8C& pvid = QAEnPXeg.IuO7iSzUg9wQIc1LBPk3uWh8ABnsa' 
    $ xml_data = file_get_contents($ data_url);

    $ inspect = 256;
    echoFirst $ inspect bytes out of,count($ xml_data),:\\\
    ,wordwrap(substr($ xml_data,0,$ inspect)),\\\

    echoUTF-8 test:,var_dump(can_be_valid_utf8_statemachine($ xml_data)),\\\
    ;

    $ simple_xml = simplexml_load_string($ xml_data,null,LIBXML_NOCDATA);
    var_dump($ simple_xml);


    / **
    如果它将验证
    *作为utf-8,则按位检查字符串。
    *
    * @param string $ str
    * @return bool
    * /
    function can_be_valid_utf8_statemachine($ str){
    $ length = strlen str);
    for($ i = 0; $ i $ c = ord($ str [$ i]);
    if($ c< 0x80)$ n = 0; #0bbbbbb
    elseif(($ c& 0xE0)== 0xC0)$ n = 1; #110bbbbb
    elseif(($ c& 0xF0)== 0xE0)$ n = 2; #1110bbbb
    elseif(($ c& 0xF8)== 0xF0)$ n = 3; #11110bbb
    elseif(($ c& 0xFC)== 0xF8)$ n = 4; #111110bb
    else return false; #不匹配
    ($ j = 0; $ j< $ n; $ j ++){#n字节匹配10bbbbb跟随?
    if((++ $ i == $ length)||((ord($ str [$ i])& 0xC0)!= 0x80))
    return false;
    }
    }
    return true;
    }

    我假设这将解决您的问题。如果不是DomDocument能够处理CDATA元素。由于编码链未经进一步测试,您可能仍会在进一步处理数据时遇到编码问题,因此请务必将编码保持在输出。


    I'm busy trying to process the following RSS feed: Yahoo Search RSS, using the following code once the data is fetched:

    $response = simplexml_load_string($data);

    However, 99% of the chinese characters and strings disappear when I interrogate the simple xml object.

    I've tried converting the incoming data to utf8 by doing:

    $data = iconv("UTF-8", "UTF-8//TRANSLIT", $data);

    But this also doesn't help.

    Before the data hits simplexml_load_string its 100% fine. But afterwards, its not.

    Any ideas?

    解决方案

    What you describe sounds like an encoding issue. Encoding is like a chain, if it get's broken at one part of the processing, the data can be damaged.

    When you request the data from the RSS server, you will get the data in a specific character encoding. The first thing you should find out is the encoding of that data.

    Data URL: http://tw.blog.search.yahoo.com/rss?ei=UTF-8&p=%E6%95%B8%E4%BD%8D%E6%99%82%E4%BB%A3%20%E9%9B%9C%E8%AA%8C&pvid=QAEnPXeg.ioIuO7iSzUg9wQIc1LBPk3uWh8ABnsa
    

    According to the website headers, the encoding is UTF-8. This is the standard XML encoding.

    However if the data is not UTF-8 encoded while the headers are saying so, you need to find out the correct encoding of the data and bring it into UTF-8 before you go on.

    Next thing to check is if simplexml_load_string() is able to deal with UTF-8 data.

    I do not use simplexml, I use DomDocument. So I can not say if or not. However I can suggest you to use DomDocument instead. It definitely supports UTF-8 for loading and all data it returns is encoded in UTF-8 as well. You should safely assume that simplexml handles UTF-8 properly as well however.

    Next part of the chain is your display. You write that your data is broken. How can you say so? How do you interrogate the simplexml object?


    Revisiting the Encoding Chain

    As written, encoding is like a chain. If one element breaks, the overall result is damaged. To find out where it breaks, each element has to be checked on it's own. The encoding you aim for is UTF-8 here.

    • Input Data: All Checks OK:
      • Check: Does the encoding data seems to be UTF-8? Result: Yes. The input data aquired from the data URL given, does validate the UTF-8 encoding. This could be properly tested with the data provided.
      • Check: Does the raw xml data mark itself as being UTF-8 encoded? Result: Yes. This could be verified within the first bytes that are: <?xml version="1.0" encoding="UTF-8" ?>.
    • Simple XML Data:
      • Check: Does simple_xml support the UTF-8 encoding? Result: Yes.
      • Check: Does simple_xml return values in the UTF-8 encoding? Result: Yes and No. Generally simple_xml support properties containing text that is UTF-8 encoded, however a var_dump() of the simple_xml object instance with the xml data suggests that it does not support CDATA. CDATA is used in the data in question. CDATA elements will get dropped.

    At this point this looks like the error you are facing. However you can convert all CDATA elements into text. To do this, you need to specify an option when loading the XML data. The option is a constant called LIBXML_NOCDATA and it will merge CDATA as text nodes.

    The following is an example code I used for the tests above and demonstrates how to use the option:

    $data_url = 'http://tw.blog.search.yahoo.com/rss?ei=UTF-8&p=%E6%95%B8%E4%BD%8D%E6%99%82%E4%BB%A3%20%E9%9B%9C%E8%AA%8C&pvid=QAEnPXeg.ioIuO7iSzUg9wQIc1LBPk3uWh8ABnsa';
    $xml_data = file_get_contents($data_url);
    
    $inspect = 256;
    echo "First $inspect bytes out of ", count($xml_data),":\n", wordwrap(substr($xml_data, 0, $inspect)), "\n";
    echo "UTF-8 test: ", var_dump(can_be_valid_utf8_statemachine($xml_data)), "\n";
    
    $simple_xml = simplexml_load_string($xml_data, null, LIBXML_NOCDATA);
    var_dump($simple_xml);
    
    
    /**
     * Bitwise check a string if it would validate 
     * as utf-8.
     *
     * @param string $str
     * @return bool
     */
    function can_be_valid_utf8_statemachine( $str ) { 
        $length = strlen($str); 
        for ($i=0; $i < $length; $i++) { 
            $c = ord($str[$i]); 
            if ($c < 0x80) $n = 0; # 0bbbbbbb 
            elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb 
            elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb 
            elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb 
            elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb 
            else return false; # Does not match 
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ? 
                if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80)) 
                    return false; 
            } 
        } 
        return true; 
    }
    

    I assume that this will fix your issue. If not DomDocument is able to handle CDATA elements. As the encoding chain is not further tested, you might still get encoding issues in the further processing of the data, so take care that you keep the encoding up to the output.

    这篇关于SimpleXML和中文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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