xml 有html标签 php,在PHP中解析XML内部的HTML标签

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方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值