How to Construct xml in php with Special Characters?

后端 未结 2 381
南笙
南笙 2021-01-23 09:42

My user enters message with special characters as follows:

this is a test of &&, &&, % as well as \'\' "", &         


        
相关标签:
2条回答
  • 2021-01-23 10:13

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

    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.

    0 讨论(0)
  • 2021-01-23 10:26

    You can use DOMDocument to create xml document and append required elements and text. All characters like & will be converted to & etc.

    0 讨论(0)
提交回复
热议问题