php - How to access <content:encoded> with CDATA from RSS feed? -


this question has answer here:

problem

if try access <content:encoded> in rss feed result null.

xml

<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">     <channel>         <item>         <pubdate>tue, 16 jun 2015 14:58:15 +0200</pubdate>         <title>title</title>         <link>/your/path/</link>         <description>description</description>         <content:encoded>         <![cdata[         <p>content</p>         ]]>         </content:encoded>         </item>     </channel> </rss> 

php

$data = simplexml_load_string($xml);  foreach ($data->channel->item $item){     $title = $item->title;     var_dump($title); //title     $content = $item->content;     var_dump($content); //null } 

question

how can access content , save <p>content</p>?

the element's name not content, encoded; content: "namespace prefix", bound @ top of document namespace identifier "http://purl.org/rss/1.0/modules/content/".

therefore, need use the ->children() method select right namespace:

echo $item->children('http://purl.org/rss/1.0/modules/content/')->encoded; 

(obviously, put namespace identifier variable or constant somewhere avoid pasting every time need it.)

note i've used echo here, rather var_dump; var_dump doesn't cope simplexml (or vice versa?) , among other things make cdata content missing. echo, on other hand, implicitly casts string content, works fine. in real code, you're want string content explicitly, this:

$encoded_content = (string) $item->children('http://purl.org/rss/1.0/modules/content/')->encoded; 

Comments