Help with XML, PHP and CDATA
Question
I have the following contents in my XML file:
<items>
<item id="1"><content><![CDATA[<p>string</p>]]></content></item>
</items>
I have HTML inside the content, so, I used CDATA. However, when I'm trying to display the content as HTML on the web page:
$item_content = $xpath2->query("/bulletin/item[@id='$item_id']/content");
foreach ($item_content as $i)
{
$a = $dom->createElement('div');
$a->appendChild($dom->createCDATASection($i->nodeValue));
// $child = $dom->createElement('div',$i->nodeValue); <--this fails miserably
$content_tag->appendChild($a);
}
It will get displayed as:
< ;p> ;string< ;/p> ;
How do I display it as HTML?
Solution
$a->appendChild($i->nodeValue);
instead of having:
$a->appendChild($dom->createCDATASection($i->nodeValue));
OTHER TIPS
With html_ entity_decode(), it's possible to convert the html entities to normal characters.
I think you can use the html_entity_decode() function.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow