RSS提要的元素包含HTML。正如How to parse CDATA HTML-content of XML using SimpleXML?中所述,您需要获取该元素的节点值(HTML)并在附加解析器中对其进行解析。
accepted answer to the linked question已经显示了这一点非常详细,对于SimpleXML,它在这里不起什么作用,无论这个RSS提要是使用CDATA还是只使用像你的情况的实体。
$feed = file_get_contents('http://uk.news.yahoo.com/rss');
$rss = simplexml_load_string($feed);
$dom = new DOMDocument(); // the HTML parser used for descriptions' HTML
foreach ($rss->channel->item as $item)
{
echo '
' . $item->title . '
', "\n";foreach ($item->description as $desc)
{
$dom->loadHTML($desc);
$html = simplexml_import_dom($dom)->body;
echo $html->p->a['href'], "\n";
}
}
输出例:
...
Chantal nears hurricane strength in Caribbean
http://uk.news.yahoo.com/chantal-nears-hurricane-strength-caribbean-220149771.html
Placido Domingo In Hospital With Blood Clot
http://uk.news.yahoo.com/placido-domingo-hospital-blood-clot-215427742.html
Berlusconi's final tax fraud appeal hearing set for July 30
http://uk.news.yahoo.com/berlusconis-final-tax-fraud-appeal-hearing-set-july-214714122.html
China: Men Rescued From River Amid Floods
http://uk.news.yahoo.com/china-men-rescued-river-amid-floods-213005159.html
Snowden has not yet accepted asylum in Venezuela - WikiLeaks
http://uk.news.yahoo.com/snowden-not-yet-accepted-asylum-venezuela-wikileaks-190332291.html
Three US kidnap victims break silence
http://uk.news.yahoo.com/three-us-kidnap-victims-release-thankyou-video-093832611.html
...
希望这有助于。与接受的答案相反,我认为没有理由申请htmlspecialchars_decode,实际上我很确定这会破坏事情。另外我的例子展示了如何通过展示如何在解析HTML之后将DOMNode转换回SimpleXMLElement来保持访问更多子元素的SimpleXML方式。