Jump to content

Decoding/encoding Html Characters And Entities


real_illusions

Recommended Posts

Hi,I am getting some stuff from an xml file and would like to parse html, so that the html gets read like it normally would, so <a href="">Click me!</a> would appear as a link saying "Click me!", and not either left out completely or display the html code.The text inside the xml file is as follows - Check out <a href='example.php'>this page</a> cos its awesome.And it gets displayed as Check out cos its awesome.I've tried what seems all like the combinations of htmlspecialchars, htmlentities etc functions from php.net but nothing seems to happen. :)The PHP im using is quite simple: (extract from the code that deals with what im after).

foreach($xml->news as $news) {if ($articleCount<$articleLimit) {	$text = $news[0]->item[0];	$text = nl2br($text);							echo "<h3>" . $news[0]->header[0] . "</h3><p>" . $text . "</p>";	$articleCount++;}

Link to comment
Share on other sites

Maybe encoding the string will do it????

function hexentities($str) { // encodes the text into 'html entity' format	global  $encoded;	$encoded = '';	for($i = 0; $i < strlen($str); $i++) {		$encoded .= ''.bin2hex(substr($str, $i, 1)).';';	}	return $encoded;

Link to comment
Share on other sites

The easiest thing to do from what you have here would be to store each content containing such tags as CDATA. Applying html_entity_decode() over THAT would then produce what you want.i.e.

<item><![CDATA[<a href="">Click me!</a>]]></item>

And in PHP:

$text = nl2br(html_entity_decode($news[0]->item[0]));

The other solution is a little more complicated... complicated enough to require DOM: clone the node in a new DOMDocument, and output that. For the sake of completeness (and in case you can't edit this XML), here's another solution, which partially uses DOM:

$text = dom_import_simplexml($news[0]->item[0]);$dom = new DOMDocument;$dom->appendChild($dom->importNode($text));$text = nl2br($dom->saveXML());

Note that it would probably be more efficient if you only use DOM instead of a mixture of DOM and SimpleXML. Also, I'm not sure if this last one will work. I haven't used SimpleXML enough.

Link to comment
Share on other sites

Thanks.The cdata thing works perfectly. Have also found that by using [[a href=""]] and then using str_replace the [[ and ]] into the relevent < and > characters.The DOM function there didnt output anything. I have only dealt with simpleXML and php5, so i dont know about the DOM functions in php4 and previous.:)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...