创建 URL 字符串时 XML 中的 & 符号问题 [英] Ampersand problem in XML when creating a URL String

查看:28
本文介绍了创建 URL 字符串时 XML 中的 & 符号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 XML 提要,该提要的节点之一是类似于以下内容的 URL 字符串:

I am working with an XML feed that has, as one of it's nodes, a URL string similar to the following:

http://aflite.co.uk/track/?aid=13414&mid=32532&dl=http://www.google.com/&aref=chris

我知道 & 符号会导致 XML 中的很多问题,应该使用 & 而不是裸露的 & 来转义.因此,我将 php 更改为如下所示:

I understand that ampersands cause a lot of problems in XML and should be escaped by using & instead of a naked &. I therefore changed the php to read as follows:

<node><?php echo ('http://aflite.co.uk/track/?aid=13414&amp;mid=32532&amp;dl=http://www.google.com/&amp;aref=chris'); ?></node>

但是,当这生成 XML 提要时,字符串会显示完整的 &amp;所以实际的 URL 不起作用.如果这是一个非常基本的误解,我们深表歉意,但提供一些指导会很好.

However when this generates the XML feed, the string appears with the full &amp; and so the actual URL does not work. Apologies if this is a very basic misunderstanding but some guidance would be great.

我也尝试过使用 %26 而不是 &amp; 但仍然遇到同样的问题.

I've also tried using %26 instead of &amp; but still getting the same problem.

推荐答案

如果你在 XML/HTML 中插入一些东西,你应该总是使用 htmlspecialchars 函数.这会将您的字符串转义为正确的 XML 语法.

If you are inserting something into XML/HTML you should always use the htmlspecialchars function. this will escape your strings into correct XML syntax.

但是您遇到了第二个问题.您已将第二个网址添加到第一个网址.这也需要转义为 url 语法.为此,您需要使用 urlencode.

but you are running into a second problem. your have added a second url to the first one. this need also escaped into url syntax. for this you need to use urlencode.

<node><?php echo htmlspecialchars('http://aflite.co.uk/track/?aid=13414&mid=32532&aref=chris&dl='.urlencode('http://www.google.com/')); ?></node>

这篇关于创建 URL 字符串时 XML 中的 & 符号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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