• 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/>";

  • 相关阅读:
    人机博弈,吃子棋游戏(一)基本介绍
    cesm下载备注
    mysql数据库批量高速插入
    持续学习
    顺序表的功能实现
    Broccoli &amp; Babel使用演示样例
    rk3188调试记录
    Operation not allowed on a unidirectional dataset错误?
    dbExpress操作中用TDBGrid显示数据
    dbexpress连接mysql提示Operation not allowed on a unidirectional dataset
  • 原文地址:https://www.cnblogs.com/dengcw/p/5659149.html
Copyright © 2020-2023  润新知