主页 > 编程资料 > PHP >
发布时间:2018-08-24 作者:apizl 阅读:431次

实际项目中不可避免的会有些中文目录或者文件,这个时候就要对中文的自适配了。

如下就详解在window环境下的适配过程,首先opendir读取到的目录是gb2312。


但是如果在PHP里面操作或保存到数据库那么就要转成UTF-8,如果要对中文目录或文件操作。需要将路径编码转成gb2312,例如下代码操作:


public function stringToGB2312($string)
    {
        $encode = mb_detect_encoding($string, array("ASCII", 'UTF-8', 'GB2312', "GBK", 'BIG5'));
        if($encode!='GB2312'){
            $string = iconv($encode, 'GB2312', $string); //全部转GB2312
        }
        return $string;
    }

phpzip压缩中文文件时候出现压缩无大小或无法压缩

如果需要把路径存到数据库或者修改路径就需要转UTF-8。


public function stringToUtf8($string)
    {
        $encode = mb_detect_encoding($string, array("ASCII", 'UTF-8', 'GB2312', "GBK", 'BIG5'));
        if($encode!='UTF-8'){
            $string = iconv($encode, 'UTF-8', $string); //全部转UTF-8
        }
        return $string;
    }

其实很简单,需要控制好对应编码就行。


文章由爱资料原创本文地址:https://www.apizl.com/archives/view-134318-1.html,转载请以链接形式标明本文地址!
关键字词: