zoukankan      html  css  js  c++  java
  • PHP urlencode 中文 兼容任意浏览器

    URLEncode:是指针对网页url中的中文字符的一种编码转化方式,最常见的就是Baidu、Google等搜索引擎中输入中文查询时候,生成经过Encode过的网页URL。

    URLEncode的方式一般有两种,一种是传统的基于GB2312的Encode(Baidu、Yisou等使用),另一种是基于UTF-8的Encode(Google、Yahoo等使用)。

    本工具分别实现两种方式的Encode与Decode:

    中文 -> GB2312的Encode -> %D6%D0%CE%C4

    中文 -> UTF-8的Encode -> %E4%B8%AD%E6%96%87

    注意:Firefox对GB2312的Encode的中文URL支持不好,因为它默认是UTF-8编码发送URL的,但是ftp://协议可以,我试过了,我认为这应该算是Firefox一个bug。

    HTML中的URLEncode:

    编码为GB2312的html文件中:http://s.jb51.net/中文.rar -> 浏览器自动转换为 -> http://s.jb51.net/%D6%D0%CE%C4.rar

    注意:Firefox对GB2312的Encode的中文URL支持不好,因为它默认是UTF-8编码发送URL的,但是ftp://协议可以,我试过了,我认为这应该算是Firefox一个bug。

    编码为UTF-8的html文件中:http://s.jb51.net/中文.rar -> 浏览器自动转换为 -> http://s.jb51.net/%E4%B8%AD%E6%96%87.rar

    让所有浏览器兼容的 方案:

    使用mb_convert_encoding函数,列子:

    $url='http://www.fx678.com:800/?code=编码678_%678';

    //未知原编码,通过auto自动检测后,转换编码为utf-8
    $convert_url=mb_convert_encoding($url, "UTF-8", "auto");
    echo $convert_url.'<br/>';


    $url_urle=urlencode(mb_convert_encoding($url, 'utf-8', 'auto'));
    $url_rawu= rawurlencode(mb_convert_encoding($url, 'utf-8', 'auto'));
    echo $url_urle."<br/>";
    echo $url_rawu."<br/>";


    echo urldecode($url_urle)."<br/>";
    echo rawurldecode($url_rawu)."<br/>";

    结果:

    http://www.fx678.com:800/?code=编码678_%678
    http%3A%2F%2Fwww.fx678.com%3A800%2F%3Fcode%3D%E7%BC%96%E7%A0%81678_%25678
    http%3A%2F%2Fwww.fx678.com%3A800%2F%3Fcode%3D%E7%BC%96%E7%A0%81678_%25678
    http://www.fx678.com:800/?code=编码678_%678
    http://www.fx678.com:800/?code=编码678_%678

    结论 用下面的方法就好:

    $url='http://www.fx678.com:800/?code=编码678_%678';

    $url_urle=urlencode(mb_convert_encoding($url, 'utf-8', 'auto'));
    echo $url_urle."<br/>";
    echo urldecode($url_urle)."<br/>";

  • 相关阅读:
    NGINX配置UPSTREAM实现负载均衡
    PHPSTOM 实用LARAVEL 需要附加的 命令
    LINUX 下安装RSYNC
    PHP FLUSH SLEEP 输出缓存控制详解
    YII2-搜索带分页,分页的两种方式
    [weird problem] the xm file transfered by wcf,some sections in it were always repeated
    【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题
    jqMobile中pageinit,pagecreate,pageshow等函数的执行顺序
    Jquery Mobile中pageinit等函数执行两次的问题【终极解决】
    jqMobile中的dialog和popup的区别
  • 原文地址:https://www.cnblogs.com/dengcw/p/5659149.html
Copyright © 2011-2022 走看看