且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用特殊字符在php中构造xml?

更新时间:2022-01-06 06:28:12

尝试使用 htmlspecialchars() (PHP 规范),然后将其添加到 XML 文档中,该文档应该为您提供所需的编码.

Try cleansing the input string using htmlspecialchars() (PHP spec) before adding it to the XML document which should give you the encoding you require.

但是,如果您在创建 XML 文档的方式上不受限制(即,您可以通过纯字符串操作以外的方式进行操作),我会遵循 prostynick 的解决方案,因为从长远来看,它更加强大.

However if you are not constrained in the way that you create the XML document (i.e. you can do it in a way other than pure string manipulation) I would follow prostynick's solution as it is much more robust in the long term.