如何使用查询字符串动态构建 HTML 链接?
如果您需要创建要在 HTML 链接中使用的查询字符串(例如<a href="index.php?param1='.$value.'">Link</a>
)那么你应该使用http_build_query https://www.php.net/manual/en/function.http-build-query.php。
该函数接受 4 个参数,第一个参数是查询数据的数组/对象。在大多数情况下,其他 3 个参数是无关紧要的。
$qs = [
'a' => 'a',
'quot;' => 'bar foo',
];
echo '<a href="?' . http_build_query($qs) . '">Link</a>';
然而,你仍然应该通过函数的输出htmlspecialchars https://www.php.net/manual/en/function.htmlspecialchars.php来编码&
正确。“一个好的框架会自动执行此操作,例如 Laravel 的 {{ }}”
echo '<a href="?' . htmlspecialchars(http_build_query($qs)) . '">Link</a>';
或者,您可以将第三个参数传递给http_build_query
as '&'
,留下第二个null
。这将使用&
代替&
这是什么htmlspecialchars
会做。
关于空间.
用于表单数据(即查询字符串)时,空格应编码为+在任何其他地方,它应该编码为%20 https://stackoverflow.com/a/29948396/1839439 e.g. new%20page.php?my+field=my+val
。这是为了确保与所有浏览器的向后可比性。您可以使用较新的 RFC3986 它将空格编码为%20
它可以在所有常见的浏览器中运行,并与现代标准保持同步。
echo '<a href="?' . http_build_query($qs, null, '&', PHP_QUERY_RFC3986) . '">Link</a>';
rawurlencode
vs urlencode
对于 URL 之前的任何部分?
你应该使用rawurlencode https://www.php.net/manual/en/function.rawurlencode.php。例如:
$subdir = rawurlencode('blue+light blue');
echo '<a href="'.$subdir.'/index.php">rawurlencode</a>';
如果在上面的例子中你使用了urlencode
该链接将被破坏。urlencode
用途非常有限,应避免使用。
不要传递整个 URLrawurlencode
。分离器/
URL 中的其他特殊字符如果要实现其功能,则不应进行编码。
Footnote
对于使用的最佳实践尚未达成普遍共识http_build_query
,除了它应该通过的事实之外htmlspecialchars
就像 HTML 上下文中的任何其他输出一样。
Laravel https://github.com/laravel/framework/blob/024783a3328c1e666390307e6704e64f256cb327/src/Illuminate/Support/Arr.php#L592 uses http_build_query($array, null, '&', PHP_QUERY_RFC3986)
代码点火器 https://github.com/codeigniter4/framework/blob/4fc8c8549ea009c7489183e951d7da7f8a7dcd39/system/HTTP/URI.php#L826 uses http_build_query($query)
Symfony https://github.com/symfony/symfony/blob/2c9a1960a164a857cd551881b580266bc77bdafa/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L291 uses http_build_query($extra, '', '&', PHP_QUERY_RFC3986)
Slim https://github.com/slimphp/Slim/blob/fe39e6ef0e669a0003123247e9a9da73f10fef92/Slim/Router.php#L389 uses http_build_query($queryParams)
CakePHP https://github.com/cakephp/cakephp/blob/5abb52489c3c98631c21a740ca9024c579c77b1e/src/Http/Client.php#L470 uses http_build_query($query)
Twig https://github.com/twigphp/Twig/blob/1190d3d42031d0484f421bb5dc2e8759e786caa4/src/Extension/CoreExtension.php#L608 uses http_build_query($url, '', '&', PHP_QUERY_RFC3986)