在PHP中写入xml时从字符串中删除& amp [英] Remove &amp from string when writing to xml in PHP

查看:390
本文介绍了在PHP中写入xml时从字符串中删除& amp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DOMDocument将包含&的链接写入XML文件.标志. 当我尝试此操作时,链接变为&在xml中. 因此,从product=1&qty;=1变为product=1&qty;=1.

I am trying to write into an XML file using DOMDocument a link that contains the & sign. When I try this, the link becomes & in the xml. So from product=1&qty;=1 becomes product=1&qty;=1.

您能告诉我一种避免这种情况的方法吗?

Can you please tell me a way to avoid this?

推荐答案

&符号应该这样编码.更改它是错误的.

Ampersands should be encoded like this. Changing it would be wrong.

请参见 http://www.w3.org/TR/xml/

&字符(&)和左尖括号(<)不得以其文字形式出现,除非用作标记定界符,或在注释中使用处理指令,或CDATA部分.如果在其他地方需要它们,则必须分别使用数字字符引用或字符串&amp;&lt;对其进行转义.

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings &amp; and &lt; respectively.

http://www.w3.org/TR/xhtml1/#C_12

在SGML和XML中,&字符(&")声明实体引用的开头(例如,注册商标符号®"的&reg;).不幸的是,许多HTML用户代理都默默地忽略了HTML文档中对&"字符的不正确使用-将看起来不像实体引用的&"字符视为文字&"号.基于XML的用户代理将不会容忍这种不正确的用法,并且任何不正确使用与号的文档都将不是有效"的,因此将不符合此规范.为了确保文档与历史HTML用户代理和基于XML的用户代理兼容,必须将要在文档中使用的与符号视为文字字符自己表示为实体引用(例如"&amp;").例如,当a元素的href属性引用带有参数的CGI脚本时,必须将其表示为http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user而不是http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user

In both SGML and XML, the ampersand character ("&") declares the beginning of an entity reference (e.g., &reg; for the registered trademark symbol "®"). Unfortunately, many HTML user agents have silently ignored incorrect usage of the ampersand character in HTML documents - treating ampersands that do not look like entity references as literal ampersands. XML-based user agents will not tolerate this incorrect usage, and any document that uses an ampersand incorrectly will not be "valid", and consequently will not conform to this specification. In order to ensure that documents are compatible with historical HTML user agents and XML-based user agents, ampersands used in a document that are to be treated as literal characters must be expressed themselves as an entity reference (e.g. "&amp;"). For example, when the href attribute of the a element refers to a CGI script that takes parameters, it must be expressed as http://my.site.dom/cgi-bin/myscript.pl?class=guest&amp;name=user rather than as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user

这篇关于在PHP中写入xml时从字符串中删除&amp; amp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆