htmlspecialchars + htmlentities无效

时间:2014-07-21 17:01:29

标签: php html

我在搜索页面上设置分页并尝试对每个号码进行搜索查询。

 href="?s=search+term"

问题是当用户输入#等特殊字符时,它会注释掉它背后的任何内容。

通常我会使用htmlentities将其变为%23但是在这种情况下它不起作用。 请注意,第一次搜索时,搜索查询中的内容如下所示

 href="?s=%23+search+term"

点击页码后,搜索查询就像这样

 href="?s=#%20search%20term"

当由php执行时,会被注释掉。关于如何绕过这个的任何想法?

1 个答案:

答案 0 :(得分:3)

您需要在搜索字词上使用urlencode()对其进行正确编码,以便在网址中使用。

http://php.net/manual/en/function.urlencode.php

作为更好的选择,您可以使用http_build_query()从数组生成整个查询字符串:

$params = [
    's' => "my search term",
    'p' => "3"
];
echo http_build_query($params);  // will echo a properly encoded querystring
相关问题