Re: Problem with special char in MySQL and XML [message #174841 is a reply to message #174834] |
Wed, 13 July 2011 07:06 |
alvaro.NOSPAMTHANX
Messages: 277 Registered: September 2010
Karma:
|
Senior Member |
|
|
El 12/07/2011 22:02, Sarah escribió/wrote:
> Hi! I've a table UTF8_general_ci where I've symbols like:& è , à
> ' ...
>
> I've seen that when I try to generate an XML file (that works OK with
> other tables) I show this error:
>
>
> Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]:
> unterminated entity reference Pizza in /var/www/domain.com/xmlfeed.php
> on line 164
>
>
> In my PHP code I've something like this:
>
> $aaa->addChild('date', strip_tags(utf8_encode($row{'Date'})) );
> $aaa->addChild('title', strip_tags(utf8_encode($row{'Title'})) );
You are converting to UTF-8 some database info that's stored in UTF-8.
When, how and why do you convert it to ISO-8859-1?
Whatever, I've made some tests with SimpleXML and, curiously, it fails
to encode the "&" symbol :-? I looks like you need to do it yourself
either with htmlspecialchars() or strtr().
However, there's one more thing to take into account. Since you are
using strip_tags() I guess your input data is HTML. That means that it
can possibly contain its own HTML entities. Stripping HTML tags won't
convert entities back to regular characters and changing the "&" symbol
to "&" will convert stuff like "é" to "é" so you
will end up with plain text that contains HTML entities!
Since you are already using UTF-8 at some point, perhaps the only
reliable way to get rid of HTML entities is to use
mb_convert_encoding(). Full process would be:
$html = mb_convert_encoding($row['Title'], 'UTF-8', 'HTML-ENTITIES');
$text = strip_tags($html);
$simplexml_input = strtr(text , array('&' => '&'));
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
|
|
|