Forum Moderators: coopster

Message Too Old, No Replies

decoding html characters in an xml file from database

         

surrealillusions

9:34 am on Aug 11, 2010 (gmt 0)

10+ Year Member



Hi,

I have narrowed down a problem when generating an RSS xml file from a database, to html entities.

When not encoded in the database (as in, <a href=""></a> ), then it displays fine.
When encoded in the database (as in &lt;a href=""&gt; and also &amp; and &pound; ), then it doesn't display, and when using:
$output .= "<description>" . htmlspecialchars_decode($info['content']) . "</description>";

It doesn't work. Nothing is displayed at all in the browser. In the latest entry, there is a &pound; in the description.

I need the html entities encoded properly, as some of the database content is also displayed on the web page. For the RSS xml file, the content type is:
Content-Type: application/rss+xml; charset=ISO-8859-1

Any solutions? Would utf-8 charset in the xml file work and keep the RSS valid?

optik

9:58 am on Aug 11, 2010 (gmt 0)

10+ Year Member



You shouldn't need to encode it for the database jut use xml_character_encode() to encode the data for XML, also look into the use of CDATA for XML data that still uses special characters after the encode.

surrealillusions

11:43 am on Aug 11, 2010 (gmt 0)

10+ Year Member



xml_character_encode() is an undefined function it seems.

However, on searching I found htmlentities() and that, coupled with htmlspecialchars_decode(), has appeared to work.

:)

omoutop

12:10 pm on Aug 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



// from [php.net...]

function xml_character_encode($string, $trans='')
{
$trans = (is_array($trans)) ? $trans : get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);

foreach ($trans as $k=>$v)
$trans[$k]= "&#".ord($k).";";

return mb_strtr($string, $trans);
}

I hope this helps you

optik

9:40 pm on Aug 11, 2010 (gmt 0)

10+ Year Member



Sorry I forgot it wasn't part of the PHP build but what omotoup posted is what I'm using as well.