单引号将 url 变成双胞胎?

发布于 2024-12-11 17:32:21 字数 288 浏览 0 评论 0原文

刚刚注意到,当使用单引号来回显 php 中的基本链接时,url 会重复自身。

<?php
echo '<a href=\"http://example.com\">Link URL - Single Quotes</a><br />';
?>

上面的代码将链接输出为:

http://example.com/"http://example.com/"

任何人都可以阐明其原因吗?

Just noticed that when using single quotes to echo a basic link in php, the url repeats itself.

<?php
echo '<a href=\"http://example.com\">Link URL - Single Quotes</a><br />';
?>

The above code outputs the link as:

http://example.com/"http://example.com/"

Can anyone shed some light on the reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

倚栏听风 2024-12-18 17:32:21

当您使用 ' 将字符串作为一个整体包围时,您不应该 \-转义 "。这无法创建该内容输出本身,但它可能会混淆解析器,从而产生问题,请尝试以下操作:

echo '<a href="http://example.com">Example.com</a><br />';

You shouldn't \-escape your " when you're using ' to surround the string as a whole. This couldn't create that output itself, but it might confuse a parser somewhere down the line, producing the problem. Try this instead:

echo '<a href="http://example.com">Example.com</a><br />';
疏忽 2024-12-18 17:32:21

使用 PHP 输出动态数据并保留 HTML。这将为您节省数小时的报价烦恼

?>
<a href="http://example.com">Example.com</a><br />
<?php
// carry on with the PHP

Use PHP to output dynamic data and leave the HTML out of it. This will save you hours of quotation frustration

?>
<a href="http://example.com">Example.com</a><br />
<?php
// carry on with the PHP
夏日落 2024-12-18 17:32:21
echo '<a href=\"http://example.com\">Example.com</a><br />';

输出

<a href=\"http://example.com\">Example.com</a><br />

反斜杠包含在最终输出中,很可能会导致 HTML 解析器出错。

echo '<a href=\"http://example.com\">Example.com</a><br />';

outputs

<a href=\"http://example.com\">Example.com</a><br />

The backslashes are included in the final output and most likely trip up the HTML parser.

不再让梦枯萎 2024-12-18 17:32:21

您正在转义双引号。使用单引号时不需要,反之亦然。

<?php
echo '<a href="http://example.com">Example.com</a><br />';
?>

You're escaping the double quotes. It isn't necessary when using single quotes and vice-versa.

<?php
echo '<a href="http://example.com">Example.com</a><br />';
?>
能否归途做我良人 2024-12-18 17:32:21

上面的代码输出链接为:

http://example.com/"http://example.com/"

,它不会产生该输出。

这是当您将光标放在链接上并单击链接时您在浏览器中看到的内容。解析相对和不完整的链接是浏览器工作的一部分,但大多数时候,它向用户显示的内容并不是 HTML 代码中编写的内容。

使用浏览器的“查看源代码”功能查看代码生成的 HTML。

您的代码生成的(无效)HTML 为:

<a href=\"http://example.com\">Link URL - Single Quotes</a><br />

浏览器将 \"http://example.com\" 解释为 href 属性的值。 HTTML 属性值可以用引号 (") 或撇号 (') 括起来,也可以完全不加引号,并且引号字符必须是后面的第一个非空格字符因为它在等号后面发现了一个反斜杠 (\),所以它得出结论,属性值没有被引用,并读取所有内容,直到第一个空格或直到标签结束(>) 作为属性的值

\"http://example.com\" 不是有效的 URL,浏览器将其视为不完整的 URL。不完整的 URL 需要解析为完整的 URL 才能使用。它看起来不像相对 URL(不以 .. 开头)。也没有主机名的绝对路径(不启动解决该问题的唯一方法是将其视为与当前加载的页面位于同一目录中的文件名。您网站的根目录(例如,http://example.com/index.php)。


我不会在这里为您的问题提供解决方案。这个问题已经有很多答案,可以为您提供各种方法来避免这种情况发生。

不过,请查看 PHP 手册中的字符串文档页面。您需要了解的所有信息都已在其中进行了解释。

The above code outputs the link as:

http://example.com/"http://example.com/"

No, it doesn't produce that output.

This is what you see in the browser when you put the cursor over the link and when you click on the link. It's part of the browser's job to resolve the relative and incomplete links, but what it shows to the user is, most of the times, not what it is written in the HTML code.

Use the browser's "View Source" functionality to see the HTML generated by your code.

The (invalid) HTML produced by your code is:

<a href=\"http://example.com\">Link URL - Single Quotes</a><br />

The browser interprets \"http://example.com\" as the value of the href attribute. The HTTML attribute values can be either enclosed in quotes (") or apostrophes (') or unquoted at all and the quoting character must be the first non-space character after the equal sign (=). Because it finds a backslash (\) after the equal sign, it concludes the attribute value is not quoted and read everything until the first whitespace or until the tag ends (>) as the attribute's value.

The value \"http://example.com\" is not a valid URL and the browser handles it as an incomplete URL. An incomplete URL needs to be resolved to a complete URL in order to be used. It doesn't look like a relative URL (doesn't start with ..), it doesn't look like an absolute path without a host name either (doesn't start with /). The only way to resolve it is to treat it as a file name located in the same directory as the page that is currently loaded. Chances are that your offending code runs in a page located in the root of your website (http://example.com/index.php, for example).


I won't provide a fix for your problem here. The question already have plenty of answers that provide you various ways to avoid this happen.

However, take a look at the strings documentation page in the PHP manual. All you need to know is explained there.

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