(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
- (2)读取词库
- (3)读取词库
- 金山词霸的词库读取程序
- 读取金山词霸的词库程序
- 金山词霸的词库读取程序(补充)
- Java读取星际译王(StarDict)词库[88250原创]
- 针对盘古dll重写词库读取 及新词增删改查方法
- 解决NLPIR-ICTCLAS2014分词系统词库过大,造成读取缓慢问题
- 星际译王,金山词霸,有道词典,词库下载 2
- StarDict词库
- 电气词库
- 敏感词库
- 中文词库
- 折腾词库,一个词库互转程序
- 2万4单词库SQL2005备份+真人发声MP3库,提供下载
- 搜狗词库爬虫(2):基础爬虫框架的运行流程
- 词库的构建
- PHP名词库
- 内存调试利器 valgrind
- RelativeLayout布局
- java 从Java视角理解CPU缓存(CPU Cache)
- 安装redis入门学习
- C语言的规范书写..【Pnoter】
- (2)读取词库
- ym—— Android 5.0学习之动画
- 学习笔记(6)-----用户管理系统v1.0
- (3)读取词库
- Windows Server 2008 相关下载
- 用Swift实现一款天气预报APP(二)
- UVa 571 - Jugs
- 【HDU 1081】To The Max(求子矩阵元素和)
- 【转】Sublime Text 3 安装Package Control