如何用特殊字符在php中构造xml? [英] How to Construct xml in php with Special Characters?
问题描述
我的用户输入带有特殊字符的消息,如下所示:
My user enters message with special characters as follows:
this is a test of &&, &&, % as well as '' "", " instead of this is a test of &, &&, % as well as '' "", "
我需要按如下方式构造 XML:
I need to construct XML as follows:
<?xml version='1.0' encoding='iso-8859-1'?>
<success>
<message>
this is a test of &, &&, % as well as '' "", " instead of this is a test of &, &&, % as well as '' "", "
</message>
</success>
任何机构可以帮助我吗?提前致谢
Can any body help me? Thanks in advance
推荐答案
尝试使用 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.
这篇关于如何用特殊字符在php中构造xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!