FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Problem with special char in MySQL and XML
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Problem with special char in MySQL and XML [message #174834] Tue, 12 July 2011 20:02 Go to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
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'})) );


Where is it my error?

Thanks
Re: Problem with special char in MySQL and XML [message #174836 is a reply to message #174834] Tue, 12 July 2011 23:23 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
I've seen that problem is when I've & char in my fields!!

How can I resolve this problem??

thanks
Re: Problem with special char in MySQL and XML [message #174838 is a reply to message #174834] Wed, 13 July 2011 00:19 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 12 Jul 2011 13:02:54 -0700, Sarah 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'})) );
>
>
> Where is it my error?

Perhaps wrap the data in a call to either htmlentities() or
htmlspecialchars() - you won't need both, I don't know which would be
best for your purpose, but I suspect htmlentities().

Rgds

Denis McMahon
Re: Problem with special char in MySQL and XML [message #174841 is a reply to message #174834] Wed, 13 July 2011 07:06 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
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
--
Re: Problem with special char in MySQL and XML [message #174842 is a reply to message #174841] Wed, 13 July 2011 07:56 Go to previous messageGo to next message
Andrea Benaglio is currently offline  Andrea Benaglio
Messages: 1
Registered: July 2011
Karma: 0
Junior Member
Thanks to all for help me!! But I don't know what I've to do for
create my XML output in the right way!

Thanks
Re: Problem with special char in MySQL and XML [message #174844 is a reply to message #174841] Wed, 13 July 2011 09:51 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
Thanks to all for help me!! But I don't know what I've to do for
create my XML output in the right way!

Thank
Re: Problem with special char in MySQL and XML [message #174845 is a reply to message #174844] Wed, 13 July 2011 11:50 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 13/07/2011 11:51, Sarah escribió/wrote:
> Thanks to all for help me!! But I don't know what I've to do for
> create my XML output in the right way!

It'd start by actually reading the answers and ask for clarifications on
the particular points that are not clear yet.

Another option is that one of us writes the complete application for
you, uploads it to your FTP and phones your customer to tell it's done.
Volunteers?


--
-- 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
--
Re: Problem with special char in MySQL and XML [message #174846 is a reply to message #174845] Wed, 13 July 2011 12:14 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
> Another option is that one of us writes the complete application for
> you, uploads it to your FTP and phones your customer to tell it's done.
> Volunteers?


Yesssss!!! Greeeat!!
I like this.
I'll send you my FTP access and my customer number!!
You'te the best!!!!!!!!!
Re: Problem with special char in MySQL and XML [message #174847 is a reply to message #174845] Wed, 13 July 2011 12:17 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
I try to resume, for try to find my error.

I've a DB with UTF_8 encode

I've saved char as & è à ' ì
is this OK?? Or Do I've save char with this ecode &(space)amp;
etcc....??? I mean: can I use è à ò ù & in my UTF_8 DB Table???

Now I've to read from table and show results in XML. Do I've to change
results that I've in DB, or keep in my format?


I think that this is a good start for know if I'm on right way!

Thanks to all for help me!
Re: Problem with special char in MySQL and XML [message #174848 is a reply to message #174847] Wed, 13 July 2011 12:37 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 13/07/2011 14:17, Sarah escribió/wrote:
> I try to resume, for try to find my error.
>
> I've a DB with UTF_8 encode

Right. The "U" in UTF-8 stands for "Unicode": that means that you can
store almost everything, including Chinese characters if you want.


> I've saved char as& è à ' ì
> is this OK?? Or Do I've save char with this ecode&(space)amp;
> etcc....??? I mean: can I use è à ò ù& in my UTF_8 DB Table???

These ("á", "&", "Ñ"...) are called HTML entities. They
are used in HTML (and, if I'm not wrong, also in XML). But they have
absolutely no direct relation with MySQL or relational databases.
Whatever book or tutorial you read that suggested so is a piece of junk.

> Now I've to read from table and show results in XML. Do I've to change
> results that I've in DB, or keep in my format?


I suppose you can apply the exact steps I told you and see what happens.

I can't answer that unless you explain what "format" means for you in
this context. If you mean "encoding", changing it to something that's
not UTF-8 will only make things harder. A general rule for programming
and life: do something when you have a reason to.



--
-- 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
--
Re: Problem with special char in MySQL and XML [message #174849 is a reply to message #174847] Wed, 13 July 2011 12:42 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Sarah)

> I try to resume, for try to find my error.
>
> I've a DB with UTF_8 encode
>
> I've saved char as & è à ' ì
> is this OK?? Or Do I've save char with this ecode &(space)amp;
> etcc....??? I mean: can I use è à ò ù & in my UTF_8 DB Table???

Yes, that's what UTF-8 is for. You can (and should!) store every char
literally and _not_ as character references like the &-thing.

> Now I've to read from table and show results in XML. Do I've to change
> results that I've in DB, or keep in my format?

XML uses UTF-8 by default, so you can directly output your text. But
there's still a handful of special characters that need to be written as
entity references, for example the ampersand '&'. If this char appears
unescaped in your text, it will break your XML.

In your code example from your first posting there was something like:

… strip_tags(utf8_encode(…))

I don't think either of them is necessary. Take your DB data, pass it
through htmlspecialchars() and take the result to build your XML.

Micha
Re: Problem with special char in MySQL and XML [message #174850 is a reply to message #174849] Wed, 13 July 2011 12:56 Go to previous message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
Thanks for to all for fast replies and for help me!!

So... reading your replies now I know that in my DB I've to save my
literally chars: è à & ù ì etc.


--
But there's still a handful of special characters that need to be
written as
entity references, for example the ampersand '&'. If this char appears
unescaped in your text, it will break your XML.
--


--
I don't think either of them is necessary. Take your DB data, pass it
through htmlspecialchars() and take the result to build your XML.
--

What I've to do exactly??
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: xml rpc request with mynewsletter builder
Next Topic: how to implement AJAX to html
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 04:50:32 GMT 2024

Total time taken to generate the page: 0.02873 seconds