链接描述文本中的“&”符号?
我正在使用 http://validator.w3.org 验证我的网站,但遇到了 &
在我的链接描述文本中。
这取自来源:
<a class='tag' href='/php/iphone software.php?function=developer&dev=witch%26wizards+inc.&store=143441'>witch&wizards inc.</a>
这在验证器中给出了这个错误:
第 188 行,第 540 列:无法为一般实体“向导”生成系统标识符
…6wizards+inc.&store=143441'>witch&wizards inc.
✉ 在文档中找到实体引用,但没有定义该名称的引用
如果我对描述进行 urlencode,则验证通过,但用户随后会看到显示的 urlencoded 文本,即
开发者女巫%26wizards+inc.
但是,我相信如果以未编码的方式显示它会更加用户友好,即
开发商witch&wizards inc.
有没有办法通过验证,但仍然显示用户友好的文本?
I am validating my site using http://validator.w3.org and have an issue where there is an &
in my link description text.
This is taken from the source:
<a class='tag' href='/php/iphone software.php?function=developer&dev=witch%26wizards+inc.&store=143441'>witch&wizards inc.</a>
This gives this error in the validator:
Line 188, Column 540: cannot generate system identifier for general entity "wizards"
…6wizards+inc.&store=143441'>witch&wizards inc.
✉ An entity reference was found in the document, but there is no reference by that name defined
If I urlencode the description then the validation passes, but the user then sees the text displayed urlencoded, ie
Developer witch%26wizards+inc.
However, I believe it's much more user friendly if this was displayed unencoded, ie
Developer witch&wizards inc.
Is there a way to pass validation, but still have user friendly text displayed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单:
对于作为查询字符串值一部分的&符号,URL 将它们编码为
%26
。用于将&符号显示为文本,或用于分隔查询字符串键值对的&符号 - 换句话说,对于几乎所有其他内容 - HTML将它们编码为其实体
&
。您的 HTML 应该如下所示:
Simple:
For ampersands as part of query string values, URL encode them to
%26
.For displaying ampersands as text, or ampersands used to separate query string key-value pairs — in other words, for almost everything else — HTML encode them to their entities
&
.Your HTML should look like this:
尝试使用
&
而不是%26
编辑:woah, &是在SO上扩展的。
Try using
&
rather than%26
EDIT: woah, & is expanded on SO.
将 URL 中的
&
编码为%26
,而不是&
(也将空格编码为%20
) code>) 并将显示文本中的&
编码为&
:请注意,URL 编码,用于对 URL 和 HTML 实体,用于对 HTML 中显示的文本进行编码。例如,如果您想在 HTML 中显示文本
,您可以将其编码为<br/>
,但在URL 为%3Cbr%2F%3E
。使用
htmlentities()
将特殊字符编码为 HTML PHP 中的实体。Encode
&
in the URL as%26
, not as&
(also encode the space as%20
) and encode the&
in the displayed text as&
:Note that there is a difference between URL encoding which are used to encode URLS and HTML entities used to encode displayed text in HTML. For example, if you wanted to display the text
<br/>
in HTML you would encode it as<br/>
, but in a URL it would be%3Cbr%2F%3E
.Use
htmlentities()
to encode special characters as HTML entities in PHP.