【问题标题】:How do I make the ö character appear properly in an XML file created via PHP? [duplicate]如何使 ö 字符正确显示在通过 PHP 创建的 XML 文件中? [复制]
【发布时间】:2011-07-15 07:02:29
【问题描述】:

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

这是一段sn-p代码:

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

row{'char'} 正在从数据库中拉回德语字符 ö。在 PHP 中,如何将其转换为正确的编码以便在 XML 中正确使用?

是否有 PHP 函数可以根据需要将所有内容转换为正确的 XML 格式?还是我需要一个字一个字地做,像这样?

echo "<format>".str_replace("&", "&amp;", $row{'format'})."</format>";

感谢您的帮助!

【问题讨论】:

  • XML处理指令是否包含编码,例如&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  • 您的数据库中的编码是什么?异8859-1? utf-8?

标签: php xml utf-8 formatting character-encoding


【解决方案1】:

如果不知道您的数据库中有什么编码以及您希望在 XML 输出中使用什么编码,很难具体说明,但 iconv 函数可能对进行转换很有用。

还有。您应该真正考虑使用 XML DOM,而不是使用 echo 输出 xml-as-plaintext。查看例如Reading and writing the XML DOM with PHP 。如果你不这样做,你很可能会在你的 xml 输出中遇到其他奇怪的问题。

相信我,我去过那里。 :-)

【讨论】:

    【解决方案2】:

    应该通过 htmlentities() 传递从数据库中提取的数据。它将“ö”更改为"&amp;ouml;"

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

    The PHP Manual

    【讨论】:

    • XML 文档不一定是 HTML 文档;将 'ö' 编码为 ö可能无效或产生预期结果。
    • 是的,我的错。我在重新阅读问题后意识到。
    猜你喜欢
    • 2013-12-15
    • 2012-11-25
    • 1970-01-01
    • 2015-03-23
    • 2011-12-30
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多