在网上找的,先留下来,以备后用,看评论说,这个是针对UTF-8编码的
暂时找到了两种最常用的方法
// 第一种 直接URL编码 比较方便
JS:
- <script type=”text/javascript”>
- string = encodeURIComponent(string);
- location.href = index.php?keyword=’+string;
- </script>
php:
- $keyword = (isset($_GET['keyword'])) ? $_GET['keyword'] : ”;
//第二种使用escape编码比较复杂 但兼任性似乎要好,通过ajax传递的时候可能需要
JS:
- ……..
- $.getJSON(“admin.php?action=”+escape(action),function(json){
- })
- …………
PHP:
- function unescape($str) { //定义unescape函数
- $str = urldecode($str);
- preg_match_all(“/(?:%u.{4}|&#x.;|&#d+;|.+)/U”,$str,$r);
- $ar = $r[0];
- foreach($ar as $k=>$v) {
- if(substr($v,0,2) == “%u”)
- $ar[$k] = iconv(“UCS-2BE”,”utf-8″,pack(“H4″,substr($v,-4)));
- elseif(substr($v,0,3) == “&#x”)
- $ar[$k] = iconv(“UCS-2BE”,”utf-8″,pack(“H4″,substr($v,3,-1)));
- elseif(substr($v,0,2) == “&#”) {
- $ar[$k] = iconv(“UCS-2BE”,”utf-8″,pack(“n”,substr($v,2,-1)));
- }
- }
- return join(“”,$ar);
- }
- $action=unescape($_GET["action"]);