0

我的数据库中有一个字符串,例如中华武魂当我发布通过我的网站检索数据的请求时,我以格式将数据发送到服务器%E4%B8%AD%E5%8D%8E%E6%AD%A6%E9%AD%82

我必须采取哪些解码步骤才能将其恢复为可用形式?同时清理用户输入以确保他们不会尝试 SQL 注入攻击?(编码之前或之后的转义字符串?)

编辑:

 rawurldecode();  // returns "中åŽæ­¦é­‚"
 urldecode();     // returns "中åŽæ­¦é­‚"


public function utf8_urldecode($str) { 
    $str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str)); 
    return html_entity_decode($str,null,'UTF-8'); 
}
 // returns "中åŽæ­¦é­‚"

...当我尝试在 SQL 语句中使用它时,它实际上有效。

我想是因为我在做一个echo并且die(); 没有指定 UTF-8 的标头(因此我猜这对我来说是拉丁语)

谢谢您的帮助!

4

2 回答 2

2
于 2011-01-29T18:59:09.797 回答
1

Use PHP's urldecode: http://php.net/manual/en/function.urldecode.php

You have choices here: urldecode or rawurldecode.

If you had encoded your string using urlencode, you must use urldecode because of the way spaces are handled. While urlencode converts spaces to +, it is not the same with rawurlencode.

于 2011-01-29T19:00:42.900 回答