用百度搜索时百度把搜索的中文关键字进行了gb231编码,用js如何把这个gb2312编码还原成汉字

解决方案 »

  1.   

    Server.UrlDecode("试试") 
    Server.UrlEncode("试试")
    lz自己试吧,应该是2个中的一个,好像是UrlEncode...
      

  2.   

    <%@ Language=VBScript %>
    <%
    ' 从URL中获取关键词
    function findKeystr(urlstr)
      dim regEx,vKey,vP,findKeystr1
      findkeystr=""
      vP = "(?:yahoo.+?[\?|&]p=|openfind.+&q=|google.+[\?|&]q=|lycos.+?query=|aol.+?query=|onseek.+?keyword=|search\.tom.+?word=|search\.qq\.com.+?word=|zhongsou\.com.+?word=|search\.msn\.com.+?q=|yisou\.com.+?p=|sina.+?word=|sina.+?query=|sina.+?_searchkey=|sohu.+?word=|sohu.+?key_word=|sohu.+?query=|163.+?q=|baidu.+[\?|&]wd=|3721\.com.+?name=|Alltheweb.+?q=)([^&]*)"
      set regEx=new regexp
      regEx.Global = true
      regEx.IgnoreCase = true
      regEx.Pattern = vP  set Matches = regEx.Execute(urlstr)
      for each Match in Matches
    ' 没有使用subMatches是因为有的服务器可能并没有安装VBS5.5版本
    findKeystr1 = regEx.replace(Match.value,"$1")
      next  if findKeystr1<> "" then
        findkeystr=lcase(decodeURI(findkeystr1))
        if findkeystr = "undefined" then
      findkeystr = URLDecode(findKeystr1)
        end if
      end if
    end function' 解开URL编码的函数(这是别人写的,我查到的地方标注为: 来源: CSDN  作者: dyydyy )
    Function URLDecode(enStr)
      dim deStr
      dim c,i,v
      deStr=""
      for i=1 to len(enStr)
        c=Mid(enStr,i,1)
        if c="%" then
          v=eval("&h"+Mid(enStr,i+1,2))
          if v<128 then
            deStr=deStr&chr(v)
            i=i+2
          else
            if isvalidhex(mid(enstr,i,3)) then
              if isvalidhex(mid(enstr,i+3,3)) then
                v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
                deStr=deStr&chr(v)
                i=i+5
              else
                v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
                deStr=deStr&chr(v)
                i=i+3 
              end if 
            else 
              destr=destr&c
            end if
          end if
        else
          if c="+" then
            deStr=deStr&" "
          else
            deStr=deStr&c
          end if
        end if
      next
      URLDecode=deStr
    end function
    function isvalidhex(str)
      isvalidhex=true
      str=ucase(str)
      if len(str)<>3 then isvalidhex=false:exit function
      if left(str,1)<>"%" then isvalidhex=false:exit function
      c=mid(str,2,1)
      if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
      c=mid(str,3,1)
      if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
    end functionResponse.write findKeystr("http://www.google.com/search?hl=zh-CN&q=%E4%B8%AD%E5%9B%BDe%E5%BA%B7&btnG=Google+%E6%90%9C%E7%B4%A2&lr=")%>
    <script language="javascript" runat="server" type="text/javascript"> 
    //解码URI
    function decodeURI(furl){
    var a=furl;
    try{return decodeURIComponent(a)}catch(e){return 'undefined'};
    return '';
    }
    </script>  
    --Stolen from Jiang Statistics 
      

  3.   

    http://www.cnitblog.com/yemoo/archive/2007/01/15/21883.html<script language="javascript" runat="server">
    function encode(str){
       return escape(str);
    }function decode(str){
       return unescape(str);
    }
    </script>
      

  4.   

    百度的编码和解码 
    现在做一个网站流量计数器,发现以下问题: 我在提取来源页面地址,发现google过来的关键字存放数据库时编码不正常,而百度过来的是正常的。 例如从百度过来的:http://www.baidu.com/s?wd=%C4%E3%BA%C3&cl=3 关键字:%C4%E3%BA%C3 解码后存放数据库:你好 这个是正常的 而Google过来的:http://www.google.cn/search?complete=1&hl=zh-CN&q=%E4%BD%A0%E5%A5%BD&meta= 关键字:%E4%BD%A0%E5%A5%BD 存放数据库就是乱码。 我知道google是采用utf-8格式编码的百度应该是gb2312的,我在webconfig里设置的格式是GB2312后来我把webconfig 编码格式改成为utf-8结果就相反了, google上的又能正确转换了,而百度上的就不行了。 请问各位大哥有什么方法帮小弟解决这个问题吗?谢谢啦你获取路径的时候先把路径编码成你想要的编码方式 然后再获取关键字 HttpUtility.UrlEncode(路径System.Text.Encoding.GetEncoding("gb2312")Request.Params["keyWord"].ToString() 在这个时候已经被解码了没办法用 HttpUtility.UrlEncode(Request.Params["keyWord"].ToString()System.Text.Encoding.GetEncoding("gb2312")System.Collections.Specialized.NameValueCollection requestUrl = HttpUtility.ParseQueryString(Request.Url.Query Encoding.GetEncoding("gb2312")); String baiduKeyword = requestUrl["keyword"]; 或者 Response.ContentEncoding 先使用GB2312,获取后再转回UTF-8
      

  5.   

    用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js用js
      

  6.   

    搜索引擎的url可能是任何编码。你光靠拢gb2312太狭窄了。
      

  7.   

    宽窄不是我的问题用js把经过gb2312编码的中文在转成中文这是我的问题回答别人的问题,最起码得知道我问得是啥呀。对不,先谢谢楼上顶的几位
      

  8.   

    return unescape(str); 
    前面不是已经讲了吗?
      

  9.   

    你们回答问题的人,试了吗?
    我在百度中输入:中
    http://www.baidu.com/s?wd=%D6%D0
    这样输出:中
    Response.Write(Server.UrlEncode("中"));
    得到%e4%b8%ad js的escape同样不行。
      

  10.   

    using System.Text;
    Response.Write(Server.UrlEncode("中",Encoding.UTF8));
    这个方法有错误,谁知道的正确的方法,试一试百度用得什么编码方式。