链接描述文本中的“&”符号?

发布于 2024-10-09 22:39:52 字数 785 浏览 3 评论 0原文

我正在使用 http://validator.w3.org 验证我的网站,但遇到了 & 在我的链接描述文本中。

这取自来源:

<a class='tag' href='/php/iphone software.php?function=developer&amp;dev=witch%26wizards+inc.&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟雨扶苏 2024-10-16 22:39:52

简单:

对于作为查询字符串一部分的&符号,URL 将它们编码为%26

用于将&符号显示为文本,或用于分隔查询字符串键值对的&符号 - 换句话说,对于几乎所有其他内容 - HTML将它们编码为其实体&

您的 HTML 应该如下所示:

<a class='tag' href='/php/iphone%20software.php?function=developer&dev=witch%26wizards+inc.&store=143441'>witch&wizards inc.</a>

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:

<a class='tag' href='/php/iphone%20software.php?function=developer&dev=witch%26wizards+inc.&store=143441'>witch&wizards inc.</a>
深府石板幽径 2024-10-16 22:39:52

尝试使用 & 而不是 %26

编辑:woah, &是在SO上扩展的。

Try using & rather than %26

EDIT: woah, & is expanded on SO.

梦在夏天 2024-10-16 22:39:52

将 URL 中的 & 编码为 %26,而不是 &(也将空格编码为 %20) code>) 并将显示文本中的 & 编码为 &

<a class='tag' href='/php/iphone%20software.php?function=developer%26dev=witch%26wizards+inc.%26store=143441'>witch&wizards inc.</a>

请注意,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 &:

<a class='tag' href='/php/iphone%20software.php?function=developer%26dev=witch%26wizards+inc.%26store=143441'>witch&wizards inc.</a>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文