使用特殊字符解码用户输入
我有一个搜索脚本,用户在其中输入一些通过 GET 发送的查询词。如果用户输入包含特殊字符,例如 äpple
,则 url 变为 search.php?q=äpple
并且一切正常。但是,如果用户重新运行此 URL search.php?q=äpple
,浏览器地址字段将显示 search.php?q=%E4pple
并且查询失败。我应该如何解码该字符串。 urldecode
不起作用。我以为这正是它的用途,那么还有其他问题吗?或者我还可以尝试哪些其他功能?
更新 如果我在 search.php?q=%E4pple
上使用 utf8_encode 它会生成正确的 ä
char 但如果 url 中的字符串是通过 GET 发送的,因此是正确的(并且工作)ä
已经,utf8_encode 把它弄乱了。
我想我可以提出一个条件,在做某事之前检查编码,但这不是更简单的方法吗?
I have a search script where the user input some querywords that are sent with GET. If the user input contains special characters e.g. äpple
the url becomes search.php?q=äpple
and everything works fine. But if the user reruns this url search.php?q=äpple
the browser address field shows search.php?q=%E4pple
and the query fails. How should I decode that string. urldecode
did not work. I thought this was exactly what it was for so is something else wrong? Or which other functions can I try?
UPDATE
if I use utf8_encode on the search.php?q=%E4pple
it produces the right ä
char but if the string in the url is sent by GET and thus correctly (and working) ä
already, the utf8_encode messes it up.
I guess I can make a condition were I check the encoding before doing something but isnt it an easier way??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过检查 $_GET['q'] 的编码来解决这个问题,并根据编码检查的结果进行适当的处理。请参阅http://php.net/manual/en/function.utf8-encode。 php 用于检查编码的函数。
Solved it by checking the encoding of $_GET['q'] and handle it appropriately for the result of the encode check. See http://php.net/manual/en/function.utf8-encode.php for functions that check encode.