用php获取[cdata]内容

时间:2012-01-09 22:05:18

标签: php xml simplexml

我正在阅读本教程,使用PHP Simple XML获取RSS源 原因我想自己做而不是Wordpress插件是因为缺少显示图像 使用本教程,我认为我将能够自己浏览节点并显示我想要的内容,但我甚至无法获取我需要的数据,因为它包含在![CDATA]中:

 <description>
<![CDATA[
<img src="http://images.highspeedbackbone.net/SKUimages/small/m17-1023-main666-tmc.jpg"/><br/> The Microsoft Office Home and Business 2010 Product Key Card brings together the roles of managing a business, running a household and helping with ho
]]>
</description>

所以我在那个img标签之后,但它并没有使用教程中的代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://rss.linksynergy.com/promo.rss?promoid=3076&token=8c86517642801a8d4e1132xxxxxxxxxxxxxxxx35e66a3');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($data);

欢迎任何建议。

谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

$xml = new SimpleXMLElement($data, LIBXML_NOCDATA);

这会将CDATA-Elements解析为普通文本节点,因此您可以像使用$xml['decription']

的任何其他文本一样访问它们

请参阅http://www.php.net/manual/en/simplexmlelement.construct.php以供参考。