简体   繁体   English

htmlspecialchars + htmlentities不起作用

[英]htmlspecialchars + htmlentities not working

I'm setting up pagination on a search page and trying to the search query to each number. 我正在搜索页面上设置分页,并尝试对每个数字进行搜索查询。

 href="?s=search+term"

The problem is when a user enters special characters such as #, it will comment out anything behind it. 问题是当用户输入特殊字符(例如#)时,它将注释掉其后面的所有内容。

Normally I use htmlentities to turn it into %23 however it is not working in this situation. 通常,我使用htmlentities将其转换为%23,但是在这种情况下它不起作用。 Keep in mind that the first time it searchs it looks like this in the search query 请记住,第一次搜索时,它在搜索查询中看起来像这样

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

and upon clicking a page number the search query then looks like this 在单击页码后,搜索查询将如下所示

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

Which, when executed by php, is commented out. 由php执行时,将其注释掉。 Any ideas on how to bypass this? 关于如何绕过这个的任何想法?

You'll need to use urlencode() on the search term to properly encode it for use in a url. 您需要在搜索字词上使用urlencode()才能对其进行正确编码以便在url中使用。

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

As a better option, you can generate the entire querystring from an array using http_build_query() : 作为更好的选择,您可以使用http_build_query()从数组生成整个查询字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM