# php去除文本重复、按长度排序

**URL:** <https://forum.90sec.com/t/topic/3138>\
**Category:** 工具资源\
**Created:** [2012 年1 月 9 日 17:41 UTC](https://forum.90sec.com/t/topic/3138 "2012-01-09T17:41:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

作者： ![user14](https://forum.90sec.com/user_avatar/forum.90sec.com/user14/32/11344_2.png) [@user14](https://forum.90sec.com/u/user14)\
发布日期： [2012 年1 月 9 日 17:41 UTC](https://forum.90sec.com/t/topic/3138/1 "2012-01-09T17:41:16Z")

</div>

_本帖最后由 lostwolf 于 2012-2-8 03:46 编辑_

```php
<?php
error_reporting(0);
@ini_set('memory_limit','-1');//内存无限
set_time_limit(0);
echo" [+] 去除文本重复工具"."\r\n\r\n";
echo" [+] 特性:消除重复按照字符长短排序"."\r\n\r\n";;
echo"\n"."[!] 输入要整理的文件:"."\n";
$dic=trim(fgets(STDIN));

function cmp($a, $b)//按照字符长度排序
{
        return (strlen($a)==strlen($b))?(($a==$b)?0:($a < $b) ? -1 : 1):(strlen($a) < strlen($b)) ? -1 : 1;
}

$file=file($dic);
$array=preg_replace('/($\s*$)|(^\s*^)/m','',$file); //消除空行
foreach ($array as $key=>$r){
        
        $array[$key]=trim("$r"); //去除空白字符
        
}

$new_array=array_values(array_unique($array));//消除重复行

usort($new_array, "cmp");

$new_filename="new_". basename($dic);

if(file_put_contents("$new_filename",join("\r\n",$new_array))){
        
        $num=count($file);
        $new_num=count($new_array);
        $counts=$num-$new_num;
        $files=dirname( __FILE__ ).DIRECTORY_SEPARATOR.$new_filename;
       
echo<<<INFO
\r\n
   [+] 去除重复完毕!    
 +--------------------------------------------------------.
 \r\n
  [+] 新文件为:$files \r\n   
  [+] 原始字典数量:$num 行 \r\n                     
  [+] 整理后为:$new_num 行 \r\n                   
  [+] 共替换了 $counts 行 \r\n                     
 +--------------------------------------------------------.
INFO;

}

else{  
        echo"------------------------------------------"."\r\n";
        echo"
- 错误!"."\r\n\r\n";
        echo"
- 找不到文件!请检查输入路径是否存在!"."\r\n";
        echo"------------------------------------------"."\r\n";
exit();
}

?>

```

---

<div class="post-metadata">

作者： ![lovehkba](https://forum.90sec.com/user_avatar/forum.90sec.com/lovehkba/32/11343_2.png) [@lovehkba](https://forum.90sec.com/u/lovehkba)\
发布日期： [2012 年1 月 9 日 20:43 UTC](https://forum.90sec.com/t/topic/3138/2 "2012-01-09T20:43:12Z")

</div>

支持下{:4\_62:}学习下

---

<div class="post-metadata">

作者： ![whoami](https://forum.90sec.com/letter_avatar_proxy/v4/letter/w/a9a28c/32.png) [@whoami](https://forum.90sec.com/u/whoami)\
发布日期： [2012 年2 月 22 日 05:36 UTC](https://forum.90sec.com/t/topic/3138/3 "2012-02-22T05:36:17Z")

</div>

ultraedit自带的去重复 也挺方便的{:4\_57:}
