dounai7148 2016-04-04 10:27
浏览 24
已采纳

在PHP中将XML代码保存为字符串

I am trying to save a XML code as a string in PHP in order to save it into a database later. However, the XML seems to always be executed no matter if I use "" or ' '.

$Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>' . $variable . '</Filter></C></Conds>';

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duanji9378 2016-04-04 10:43
    关注

    Just replace < by &lt; and > by &gt;

    $variable = 12345;
    $Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>' . $variable . '</Filter></C></Conds>';
    $temp_var1 = str_replace('<','&lt;',$Cxml);
    $temp_var2 = str_replace('>','&gt;',$temp_var1);
    echo $temp_var2;
    

    Output : <Conds><C><FieldNo>119</FieldNo><Filter>12345</Filter></C></Conds>

    Update :

    The htmlspecialchars() function generates the same output. For example:

    $Cxml = '<Conds><C><FieldNo>119</FieldNo><Filter>' . $variable . '</Filter></C></Conds>';
    echo htmlspecialchars($Cxml);
    

    Explanation :

    The HTML character encoder converts all applicable characters to their corresponding HTML entities. Certain characters have special significance in HTML and should be converted to their correct HTML entities to preserve their meanings.

    For example, it is not possible to use the < character as it is used in the HTML syntax to create and close tags. It must be converted to its corresponding &lt; HTML entity to be displayed in the content of an HTML page. HTML entity names are case sensitive.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效