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.

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号