(2)读取词库

来源:互联网 发布:设置数据标签格式 编辑:程序博客网 时间:2024/05/16 15:48

这个类的主要作用,迭代的读取文件,因为路径存在中文,所以必须转码,

同时转码后,由于windows下的路径分隔符,会对编码后的路径进行转义,所以必须先对路径名进行转换


<?phpclass Dir{private $fileList=array();public function __construct($path){$this->readFileList($path);}function readFileList($path){$path=$this->transPathSep($path);$encode=mb_detect_encoding($path, array('GB2312','GBK','UTF-8','BIG5','LATIN1'));$path=mb_convert_encoding($path, 'GB2312', $encode);//用于路径读取时用UTF编码会失败,所以先转成GB2312if ($fd=opendir($path)){while($fileName=readdir($fd)){//如果不是当前目录和上级目录if($fileName !="." && $fileName !=".."){//如果是一个文件if(is_file($path.'/'.$fileName)){$extName=pathinfo($path."/".$fileName)["extension"];if(strtolower($extName)=='txt'){//上面把路径转成了GB2312,这里再转换会UTF-8编码$temp=mb_convert_encoding($path.'/'.$fileName, 'UTF-8', $encode);$groupName=$this->groupFile($temp);$this->fileList[$groupName][]=$temp;}}//如果是一个目录,则继续递归读取else if(is_dir($path.'/'.$fileName)){$this->readFileList($path.'/'.$fileName);}}}}@closedir($fd);}public function getFileList(){return $this->fileList;}//提取单词分类,比如从A-Zprivate function groupFile($filename){$pos=strripos($filename, '/');$word=strtolower(substr($filename, $pos+1, 1));return $word;}//转换window环境下路径的默认分隔符\为PHP识别更好的///因为路径名中包含汉字时,必须转换为gb2312时,php才能识别//而在转换为gb2312后,如果路径名是以windows下的\分隔的,则被转换为gb2312的中文会被转义//最后就会导致读取对应的路径失败//******所以在{路径名}中包含中文时,一定要先转换路径分隔符,然后转换为gb2312编码private function transPathSep($path){$system=$_SERVER["SERVER_SOFTWARE"];$pat="#\((.*?)\)#";$sysVer=null;if(preg_match($pat,$system,$match)){$sysVer=$match[1];}else{die("匹配系统类型失败<br />");}if(strtolower($sysVer)=="win32"){$realPath=str_replace("\\","/",$path);return $realPath;}}}/* $dir=new Dir('E:\CodeEdit\php\ciba\TXT格式的牛津电子词典\牛津电子词典');$list=$dir->getFileList();echo "<pre>";print_r($list); echo "</pre>";  */?>


0 0
原创粉丝点击