xml Feed数据中的字符编码

时间:2014-12-17 18:50:25

标签: php xml utf-8 character-encoding

我正在从这样的远程源加载XML Feed:

if ($reader = XMLReader::open($url)) {    
    while ($reader->read()) {
        if ( $reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'MainNode' ) 
        {
            $node   = $reader->expand();
            $dom    = new DomDocument();
            $n      = $dom->importNode($node,true);
            $dom->appendChild($n);
            $xml    = simplexml_import_dom($n);
            echo($xml->Remarks); // problem here
         }
    }
}

当我在浏览器中查看该页面时,它在页面中有不好的字符,如下所示:

“city within a cityâ€

如果我使用Chrome工具并将页面编码从Western更改为UTF8,问题就会消失,所有字符看起来都是正确的:“city within a city”

当我直接将原始字符串插入我的数据库(UTF8数据库),然后将其显示在一个页面上(也编码为utf8)时,它显示如下:“city within a cityâ€。如果我尝试在字符串上使用utf8_encode,它看起来像这样:“city within a cityâ€Â。如果我使用utf8_decode,则会显示?city within a city?

我该如何处理?

1 个答案:

答案 0 :(得分:0)

看起来你有特殊的(")'为什么不只是str_replace那些。

$output = str_replace('Your Special Quotes', '"', $string);

这是微软所有有趣角色的功能

function convert_smart_quotes($string) 
{ 
    $search = array(chr(145), 
                    chr(146), 
                    chr(147), 
                    chr(148), 
                    chr(151)); 

    $replace = array("'", 
                     "'", 
                     '"', 
                     '"', 
                     '-'); 

    return str_replace($search, $replace, $string); 
}

然后是utf8_decode

您还可以尝试htmlspecialchars

这也是一个有用的链接http://shiflett.org/blog/2005/oct/convert-smart-quotes-with-php