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

在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.

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

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据