【发布时间】:2013-10-22 21:15:35
【问题描述】:
编辑:对于其他阅读者,这已解决。锚点 ID 中不允许使用 %20。
使用锚 id 的通常原因是,我正在制作一个文本链接列表作为标题,这些链接只是链接到页面下方的每个相关文本块。
代码有效,没有错误等,但我无法成功编码 URL。
如果没有rawurlencode(),链接工作得很好,但是它们有我需要管理的空间(也不会验证)。
我尝试只使用 urlencode() 并且效果很好,但是它添加了 + 而不是 %20 并且我读到 + 应尽可能避免(也许这不准确但没有解释为什么 rawurlencode() 不工作)。
基本代码:
// The array of list titles
$aryList = array( "1. Communications", "2. Definitions and Conditions");
// Loop to output each list title as a link
foreach ($aryList as $strListValue)
{
echo "<a href='#".rawurlencode($strListValue)."'>$strListValue</a>";
}
// The content with the list title as the link ID (destination)
echo "<a id='".rawurlencode($aryList[0])."'>".$aryList[0]."</a>";
echo "The text is about Communications.";
在源代码中,我可以看到rawurlencode() 用 %20 替换了空格,尽管浏览器地址栏中的 URL 没有 %20 而是用空格代替。单击链接什么也不做。
使用rawurlencode()时的源码:
// The list title links
<a href='#1.%20Communications'>1. Communications</a>
<a href='#2.%20Definitions%20and%20Conditions'>2. Definitions and Conditions</a>
// The list IDs (destination)
<a id='1.%20Communications'>1. Communications</a>
<a id='2.%20Definitions%20and%20Conditions'>2. Definitions and Conditions</a>
浏览器是 Iceweasel 17.0.9 FWIW。
编辑:
根据 durrrutti 所说的进行测试,当我 rawurlencode() 仅锚点而不是 ID 时,它工作得非常好。
但是,这仍然会在 ID 中留下空格问题。
现在的源代码只编码了锚点而不是 ID:
// The list title links
<a href='#1.%20Communications'>1. Communications</a>
<a href='#2.%20Definitions%20and%20Conditions'>2. Definitions and Conditions</a>
// The list IDs (destination)
<a id='1. Communications'>1. Communications</a>
<a id='2. Definitions and Conditions'>2. Definitions and Conditions</a>
【问题讨论】: