簡體   English   中英

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

[英]How to Construct xml in php with Special Characters?

我的用戶輸入帶有特殊字符的消息,如下所示:

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 &amp;, &amp;&amp;, % as well as '' &quot;&quot;, &quot; instead of this is a test of &, &&, % as well as '' "", "
</message>
</success>

有誰能夠幫助我? 提前致謝

您可以使用DOMDocument創建xml文檔,並附加所需的元素和文本。 所有&字符都將轉換為&amp; 等等

在將輸入字符串添加到XML文檔之前,請嘗試使用htmlspecialchars()PHP spec )清理輸入字符串,這應會為您提供所需的編碼。

但是,如果您不受創建XML文檔的方式的束縛(即,您可以采用除純字符串操作之外的其他方式來進行處理),我將遵循prostynick的解決方案,因為從長遠來看,它的功能更加強大。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM