【发布时间】:2012-02-06 11:33:01
【问题描述】:
我正在为一个站点创建一个 RSS 提要。
我正在使用 SimpleXML 创建 XML 结构。当我调用 $xml->asXML(); 时,它会抛出许多警告:
ErrorException [ Warning ]: SimpleXMLElement::asXML() [simplexmlelement.asxml]: string is not in UTF-8
我不确定这个错误是什么。它正在读取的数据库表是 utf8_general_ci。我尝试在字符串上运行 utf_encode 而不是修复它。
//First create the XML root
$xml = new SimpleXMLElement('<rss version="2.0"></rss>');
//Create the Channel
$channel = $xml->addChild('channel');
//Construct the feed parameters
$channel->addChild('title', 'CGarchitect Feed');
$channel->addChild('link', Config::get('base_url'));
$channel->addChild('description', 'CGarchitect is the leading online community for architectural visualization professionals.');
$channel->addChild('pubDate', date("D, d M Y H:i:s T"));
//Get the feed items
$nodes = <....snip... >
foreach ($nodes as $node)
{
//Parse the title and description
$title = htmlentities(strip_tags($node->title));
$description = htmlentities(strip_tags($node->description));
$newItem = $channel->addChild('item');
$newItem->addChild('title', $title);
$newItem->addChild('description', $description);
$newItem->addChild('pubDate', date("D, d M Y H:i:s T", $node->published_at));
}
header('Content-Type: application/xhtml+xml');
echo $xml->asXML();
提前谢谢...
伦纳德
【问题讨论】:
-
您是否也将MySql connection encoding 设置为UTF8?
-
@Jon 是的。 mysql_client_encoding() 返回 'utf8'
-
您确定您正在使用 UTF-8 连接到数据库吗?建立连接后,第一次执行此查询:mysql_query("SET NAMES 'utf8'");
-
我添加了上面的代码,结果相同。如前所述,我运行了 mysql_client_encoding() 并返回 utf8。
-
atxba 为您提供了一个很好的提示。问题是,htmlentities() 在标准模式下不能在 utf-8 中工作。像这样使用它: htmlentities ($string,ENT_NOQUOTES, 'UTF-8');标准是 ISO-8859-1。所以你必须改变它。 “ENT_NOQUOTES”表示不会替换任何引号。对于其他值,请查看手册htmlentitie()