XML 源数据中的字符编码


character encoding in xml feed data

我正在从远程源加载XML提要,如下所示:

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 工具并将页面编码从西方更改为 UTF8,问题就会消失,字符看起来都正确:“city within a city”

当我直接将原始字符串插入我的数据库(即 UTF8 db(时,然后将其显示在页面上,该页面也被编码为 utf8,它显示为:“city within a city†.如果我尝试在字符串上使用utf8_encode,它看起来像这样:“city within a city†.如果我使用utf8_decode它会显示?city within a city?

我应该如何处理?

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

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

这是Microsoft所有有趣角色的功能

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