Unable to get content of RSS properly

时间:2016-03-02 11:03:43

标签: php html xml rss

I have a problem trying to parse RSS feed in XML. The problem arises when the strings contain the ![CDATA[ parameter. I have looked for some solutions here already, but none of them seemed to work.

The code for reading the feed looks like this:

$xmlDoc = new DOMDocument();
$xmlDoc->load($url); 
$x = $xmlDoc->getElementsByTagName('item');
$xLength = $xmlDoc->getElementsByTagName('item')->length; 

for ($i=0; $i<$xLength; $i++) {
        $itemTitle = $x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue; 
        $itemLink = $x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
        $itemDesc = $x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
}

Now when I then echo the items, everything works fine, given the CDATA is not delimiting the string.

Once the description or any other part of the text is between the CDATA, it does not read it. I tried inserting the (string) in front of the $itemDesc, but that for some reason worked but only for the first item, the next items, if they containted CDATA in description, nothing appeared.

0 个答案:

没有答案