我的用户输入带有特殊字符的邮件,如下所示:
this is a test of &&, &&, % as well as '' "", " instead of this is a test of &, &&, % as well as '' "", "
我需要按如下方式构建XML:
<?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>
任何人都可以帮助我吗? 提前致谢
答案 0 :(得分:3)
您可以使用DOMDocument
创建xml文档并附加必需的元素和文本。所有字符如&
都将转换为&
等。
答案 1 :(得分:2)
尝试使用htmlspecialchars()
(PHP spec)清理输入字符串,然后将其添加到XML文档中,该文档应该为您提供所需的编码。
但是,如果您不像创建XML文档那样受限制(即您可以采用除纯字符串操作之外的方式),我会遵循prostynick的解决方案,因为它在长期内更加强大。 / p>