RSS 提要的<description>
元素包含 HTML。就像如何使用 SimpleXML 解析 XML 的 CDATA HTML 内容?您需要获取该元素(HTML)的节点值并在附加解析器中解析它。
已接受的链接问题的答案已经表明这一点非常冗长,对于 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 '<h3>' . $item->title . '</h3>', "\n";
foreach ($item->description as $desc)
{
$dom->loadHTML($desc);
$html = simplexml_import_dom($dom)->body;
echo $html->p->a['href'], "\n";
}
}
示例输出:
...
<h3>Chantal nears hurricane strength in Caribbean</h3>
http://uk.news.yahoo.com/chantal-nears-hurricane-strength-caribbean-220149771.html
<h3>Placido Domingo In Hospital With Blood Clot</h3>
http://uk.news.yahoo.com/placido-domingo-hospital-blood-clot-215427742.html
<h3>Berlusconi's final tax fraud appeal hearing set for July 30</h3>
http://uk.news.yahoo.com/berlusconis-final-tax-fraud-appeal-hearing-set-july-214714122.html
<h3>China: Men Rescued From River Amid Floods</h3>
http://uk.news.yahoo.com/china-men-rescued-river-amid-floods-213005159.html
<h3>Snowden has not yet accepted asylum in Venezuela - WikiLeaks</h3>
http://uk.news.yahoo.com/snowden-not-yet-accepted-asylum-venezuela-wikileaks-190332291.html
<h3>Three US kidnap victims break silence</h3>
http://uk.news.yahoo.com/three-us-kidnap-victims-release-thankyou-video-093832611.html
...
希望这可以帮助。与接受的答案相反,我认为没有理由申请htmlspecialchars_decode
,实际上我很确定这会破坏事情。我的示例还展示了如何在 HTML 被解析后通过展示如何将 DOMNode 转回 SimpleXMLElement 来停留在访问更多子项的 SimpleXML 方式中。