1

我发现每当我执行以下操作时:

echo '<a href="http://" title="bla">huzzah</a>';

我最终将以下内容呈现给浏览器:

<a href="http:///" title="bla">huzzah</a>

当我链接到带有扩展名的文件时,这尤其令人讨厌,因为它会破坏链接。

任何想法为什么会发生这种情况以及我该如何解决?

更新: 对于那些询问我的确切实施的人,在这里。在我的故障排除中,我尽可能地把它简化了,所以请不要介意我在哪里将纯文本连接到纯文本......

function print_it($item) {
    echo '<div class="listItem clearfix">';
    echo '<div class="info">';
    echo '<span class="title">';
    if(isset($item[6])) {
        echo '<a href="http://" title="">' . 'me' . '</a>';
    }
    echo '</span>';
echo '</div></div>';
}

更新: 作为对 Matt Long 的回应,我粘贴了您的行,并且呈现相同的内容。

更新: 作为对 Fire Lancer 的回应,我重新尝试了最初的尝试,并将在下面向您展示。

echo substr($item[6],13) . '<br>';
echo '<a href="http://' . substr($item[6],13) . '" title="' . $item[0] . '">' . $item[0] . '</a>';

<span class="title">www.edu.gov.on.ca%2Feng%2Ftcu%2Fetlanding.html<br>
<a href="http://www.edu.gov.on.ca%2Feng%2Ftcu%2Fetlanding.html" title="Employment Ontario">Employment Ontario</a></span>

子字符串的原因是由于 URL 在其他地方通过 rawurlencode() 运行,并且链接到 http%3A%2F%2F 使页面认为它是本地/相对链接。

更新: 我粘贴了上面的回复而没有真正看它。因此,在查看源代码时 HTML 是正确的,但实际页面会在其后加上另一个斜杠来解释它。

解决方案: 这都是 rawlurlencode() 的结果。如果我一起解码或跳过编码,一切都会完美运行。关于 rawurlencode() 的一些东西使浏览器想要在其中添加一个斜杠。

4

7 回答 7

3

我从来没有这样过,你对链接的反应如何?以下所有应该工作。

echo '<a href="http://someothersite.com">Link</a>';
echo '<a href="anotherpage.php">Some page</a>';
echo '<a href="../pageinparentdir.php">Another page</a>';
etc

编辑,因为您添加了信息。

您不能只将 http:// 作为 href,即使将该链接直接输入到 html 页面也会产生这种效果。例如:
html:

 <a href="http://" title="bla">huzzah</a>

链接(在FF3中):

http:///
于 2008-10-01T15:09:51.547 回答
2

Firefox, especially, shows you the html source the way it's seeing it which is rarely the way you've sent it. Clearly something about your link or it's context is making the browser interpret a trailing slash.

I wonder if it's a side effect of the url encoding. If you rawurldecode it will that help. If there are parts of the url that need to stay encoded you could search for the slashes and just put those back.

于 2008-10-01T15:43:29.233 回答
1

错误必须在其他地方。echo逐字写入字符串。没有对任何部分进行后处理。因此,额外的斜杠会添加到代码中的其他位置(在将字符串传递给 之前echo)。

于 2008-10-01T15:10:03.603 回答
0

如果您使用双引号并像这样转义内部双引号,您会得到相同的结果吗?

echo "<a href=\"http://\" title=\"bla\">huzzah</a>";
于 2008-10-01T15:18:15.020 回答
0

如果我将该 echo 命令放在我的 PHP 代码中,它会按预期输出“http://”(您可以在生成的输出的源代码中看到它),但是当我将鼠标悬停在结果页面中的链接上时(使用 IE7 ),它显示http:///

My guess is, that that's browser behaviour, because there can't be a http:// link without a host name or IP address (you can't just access the protocol).

于 2008-10-01T15:40:28.850 回答
0

正如一些人指出的那样,“http://”不是有效链接,因此您的浏览器会在末尾添加额外的斜杠。要查看它,请尝试 lynx -dump http://yourdomain/yourfile.php(如果您有幸拥有 linux)或从您的机器 telnet 到您的服务器的 80 端口,然后输入:

GET /path/file.php HTTP/1.0

看看结果。

于 2008-10-01T15:54:34.557 回答
-1

您是否查看过您的 PHP 配置设置?可能是 magic_quotes_gpc 决定为您逃避事情(我已经被该设置咬了好几次,尤其是在使用 AJAX/JSON 流量时)。尝试确保它已关闭并再次回显(您可能需要编辑 php.ini 文件,或添加php_flag magic_quotes_gpc off到您正在工作的目录中的 .htaccess 文件,具体取决于您的环境)。

于 2008-10-01T16:04:27.217 回答