实际项目中不可避免的会有些中文目录或者文件,这个时候就要对中文的自适配了。
如下就详解在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; }
如果需要把路径存到数据库或者修改路径就需要转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; }
其实很简单,需要控制好对应编码就行。
关键字词: