Reading text in `<! [CDATA [...]]> `using SimpleXMLElement
I am importing an RSS feed with SimpleXMLElement in PHP. I have problems with the name and description. For some reason, the website to which I receive the channel places the name and description in <![CDATA[...]]> :
<item> <title><![CDATA[...title...]]></title> <link>...url...</link> <description><![CDATA[...title...]]></description> <pubDate>...date...</pubDate> <guid>...link...</guid> </item> When I do var_dump() on a SimpleXMLElement element, I get (for this part):
[2]=> object(SimpleXMLElement)#5 (5) { ["title"]=> object(SimpleXMLElement)#18 (0) { } ["link"]=> string(95) "...link..." ["description"]=> object(SimpleXMLElement)#19 (0) { } ["pubDate"]=> string(31) "...date..." ["guid"]=> string(48) "...link..." } How can I get the value in <![CDATA[...]]> to read the title and description from the feed?
SimpleXML reads CDATA nodes perfectly. The only problem you are facing is that print_r , var_dump and similar functions do not give an exact representation of SimpleXML objects, as they are not fully implemented in PHP.
If you run echo $myNode->description , you will see that the contents of the CDATA section are just fine. The reason is that when you request the conversion of SimpleXMLElement to a string, it automatically combines all the text and CDATA contents for you, but until you do this, it remembers the difference.
In general, to extract the string contents of any element or attribute in SimpleXML, enter (string)$myNode into the string using (string)$myNode . It also prevents other problems, such as functions complaining about receiving an object when they were expecting a string, or the inability to serialize when stored in a session.
See also my previous answer at fooobar.com/questions/647114 / ...