Quantcast
Channel: read cdata from a rss feed - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by IMSoP for read cdata from a rss feed

$
0
0

The above "fix" will work, but is entirely unnecessary.

SimpleXML objects contain a lot of "magic", and are not designed to be viewed using print_r; the CDATA is safely in your object, but won't show up unless you ask for it in the right way.

If you run echo (string)$movies->channel->title; you should get "Forbes.com: News" as you would expect.

Note the (string), which tells PHP to explicitly convert the "magic" SimpleXMLElement into a string. If you don't do this, you'll actually be getting another SimpleXMLElement object back - otherwise my example wouldn't work because $movies->channel would be a string.

It's good practice to always use (string) when accessing elements or attributes from SimpleXML, as some functions will choke if they are expecting a string and you give them a SimpleXML object instead, and serializing or session storage will certainly fail.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>