开发者

How do I make the ö character appear properly in an XML file created via PHP? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

php: using DomDocument whenever I try to write UTF-8 it writes the hexadecimal notation of it.

Here is a snippet of code:

echo "<char>".$row{'char'}."</char>";

row{'char'} is pulling back the german character ö from the database. In PHP, how can I convert that to the correct encoding to be used properly in XML?

Is there a PHP function that can convert everything as needed to the correct format for XML? Or do I need to do it character by character, like so?

echo "<format>".str_replace("&", "&am开发者_Python百科p;", $row{'format'})."</format>";

Thanks for the help!


Without knowing what encoding you have in your database and what encoding you want in your XML output it's hard to be specific, but the iconv function could be useful to do the conversion.

Also. you should really consider using an XML DOM instead of outputting xml-as-plaintext with echo. Check out for example Reading and writing the XML DOM with PHP . If you don't, you will most likely end up with other strange problems with your xml output down the road.

Trust me, I've been there. :-)


Passing the data pulled from the database through htmlentities() should do that. It changes "ö" to "&ouml;".

echo "<char>".htmlentities($row{'char'})."</char>";

The PHP Manual

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜