【发布时间】:2013-11-25 18:14:39
【问题描述】:
我有这个 xml 文件
http://www.metacafe.com/tags/cats/rss.xml
使用此代码:
$xml = simplexml_load_file('http://www.metacafe.com/tags/cats/rss.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo $xml->channel->item->title . "<br>";
echo $xml->channel->item->description . "<br>";
我得到这个输出:
Dad Challenges Kids to Climb Walls to Get Candy<br>
<a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/"><img src="http://s3.mcstatic.com/thumb/11150410/28824820/4/directors_cut/0/1/dad_challenges_kids_to_climb_walls_to_get_candy.jpg?v=1" align="right" border="0" alt="Dad Challenges Kids to Climb Walls to Get Candy" vspace="4" hspace="4" width="134" height="78" /></a>
<p>
Nick Dietz compiles some of the week's best viral videos,
including an elephant trying really hard to break a stick, a cat
sunbathing and kids climbing up the walls to get candy. Plus,
making music with a Ford Fiesta.
<br>Ranked <strong>4.00</strong> / 5 | 78 views | <a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/">0 comments</a><br/>
</p>
<p>
<a href="http://www.metacafe.com/watch/cb-M0fIp1ctKtsn/dad_challenges_kids_to_climb_walls_to_get_candy/"><strong>Click here to watch the video</strong></a> (02:38)<br/>
Submitted By: <a href="http://www.metacafe.com/channels/CBS/">CBS</a><br/>
Tags:
<a href="http://www.metacafe.com/topics/penna/">Penna</a>
<a href="http://www.metacafe.com/topics/bjbj/">Bjbj</a>
<a href="http://www.metacafe.com/topics/ciao/">Ciao</a> <br/>
Categories: <a href='http://www.metacafe.com/videos/entertainment/'>Entertainment</a>
</p>
<br>
我需要得到这个输出(而不是它需要删除所有其他元素):
Dad Challenges Kids to Climb Walls to Get Candy
Nick Dietz compiles some of the week's best viral videos,
including an elephant trying really hard to break a stick, a cat
sunbathing and kids climbing up the walls to get candy. Plus,
making music with a Ford Fiesta.
我不知道如何继续得到这个结果。
【问题讨论】:
-
它是 html...您已经在使用 DOM 操作来获取 xml 节点。这是一个简单的扩展,可以撕开该节点中的 html,只吸出你想要的部分。
-
你能给我举个例子吗?
-
请注意,将
LIBXML_NOCDATA传递给SimpleXML 是完全没有必要的;只要您请求元素的字符串内容,所有 CDATA 和文本节点都会被适当地展平。如果您正在执行echo以外的其他操作,则强制变量为字符串的语法是(string)$var,例如$html = (string)$xml->channel->item->description.
标签: php xml-parsing simplexml