本文发布于:2012-09-03,最后更新于:2019-11-05,如果内容失效请留言告知。
PHP处理大文本可能会出现各种性能问题,一种简单的方法是利用PHP自带的array_unique数组去重函数,另一种简单方法则为利用关联数组特性实现。下面是相关实现方法主要代码,仅供参考。
1. 利用PHP数组去重函数array_unique实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* 文本重复字符串的去重 – 方法1 利用explode和array_unique函数实现。 存在问题:file_get_contents函数将文件全部读入内存处理,这样处理速度较快,但大文件会出现内存溢出问题 */ function duplicate_remove_1( $file_input , $file_output ) { $content = file_get_contents ( $file_input ); if (! $content ) { return 0; } //符号过滤与格式处理(处理为以逗号分割的字符串) //$pattern = "/[ '.:;*?~`!@#$%^&+=-)(<>{}]|]|[|/|\|"||/"; //$content = preg_replace($pattern, '', $content); //英文符号过滤 $array = explode ( ',' , $content ); $new_array = array_values ( array_unique ( $array )); //消除重复行 $array = NULL; $num = count ( $new_array ) – 1; $content = implode( "rn" , $new_array ); $new_array = NULL; $content =preg_replace( '/($s*$)|(^s*^)/m' , '' , $content ); //消除空行 $_bool = file_put_contents ( "{$file_output}" , $content ); if (! $_bool ) { return 0; } $content = NULL; return $num ; } |
2. 利用PHP关联数组特性实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /* 文本重复字符串的去重 – 方法2 利用关联数组特性,将每个字符串作为下标,遍历文件进行赋值 存在问题:使用fopen函数循环读取文件,i/o次数多,处理速度慢 */ function duplicate_remove( $file_input , $file_output ) { $fp = fopen ( $file_input , 'r' ); if (! $fp ) { return 0; } $array = array (); while (! feof ( $fp )) { $line = fgets ( $fp , 1024); $array_line = explode ( ',' , $line ); foreach ( $array_line as $v ) //以关联数组的方式实现去重 { $array [ $v ] = $v ; } } fclose( $fp ); $content = implode( "rn" , $array ); $num = count ( $array ) -1; $array = NULL; $content =preg_replace( '/($s*$)|(^s*^)/m' , '' , $content ); //消除空行 $_bool = file_put_contents ( "{$file_output}" , $content ); if (! $_bool ) { return 0; } $content = NULL; return $num ; } |
其他参考:php简单去除大型文本重复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | < ?php error_reporting (E_ALL & ~E_NOTICE); @ ini_set ( 'memory_limit' , '-1' ); set_time_limit(0); echo " 去除文本重复工具" . "rnrn" ; echo "n" . "输入要整理的文件:" . "n" ; $dic =trim( fgets (STDIN)); /*while (!feof($dic)){ $file[]=stream_get_line($fp,65535,"rn"); } */ $filefile =file( $dic ); $array =preg_replace( '/($s*$)|(^s*^)/m' , '' , $file ); //消除空行 $new_array = array_values ( array_unique ( $array )); //消除重复行 $new_filename = "new_" . basename ( $dic ); if ( file_put_contents ( "$new_filename" ,join( "rn" , $new_array ))){ $num = count ( $file ); $new_num = count ( $new_array ); $counts = $num - $new_num ; $files =dirname( __FILE__ ).DIRECTORY_SEPARATOR. $new_filename ; echo <<<INFO +———————————————-+ | [+] 去除重复完毕! | [+] 整理后的文件为: $files | [+] 原始字典数量: $num 行 | [+] 整理后为: $new_num 行 | [+] 共替换了 $counts 行 +———————————————-+ INFO; } else { echo "——————————————" . "\r\n" ; echo "[*] 错误!" . "\r\n\r\n" ; echo "[*] 找不到文件!请检查输入路径是否存在!" . "\r\n" ; echo "——————————————" . "\r\n" ; exit (); } ?> |
第二方法还是文本太大,数组还是回变得很大,仍然有数组溢出的风险