# www字典优化脚本

**URL:** <https://forum.90sec.com/t/topic/2948>\
**Category:** 技术文章\
**Created:** [2011 年11 月 21 日 17:52 UTC](https://forum.90sec.com/t/topic/2948 "2011-11-21T17:52:56Z")\
**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)\
发布日期： [2011 年11 月 21 日 17:52 UTC](https://forum.90sec.com/t/topic/2948/1 "2011-11-21T17:52:56Z")

</div>

_本帖最后由 lostwolf 于 2011-11-29 22:07 编辑_

```php

<?php
error_reporting(0);
echo "\n"."输入要整理的字典:"."\n";
$dic= trim(fgets(STDIN));

/ ***************************************
 *字典整理 by lostwolf
 * hi.baidu.com/nginxshell
 
 * ************************************* /

$file=file($dic);

$array=preg_replace('/($\s*$)|(^\s*^)/m','', $file); //消除空行

foreach ($array as $key=>$r){
        
	$array[$key]=trim("/$r"); //添加斜杠 并去除空白字符
	
}
 
$new_array=preg_replace('#^([/]*)#','/',$array); //将开头多个斜杠替换成一个 

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

if(file_put_contents('new_file.txt',join("\r\n",$new_array1))){

	print "\n+-----------------------------------------------------+";
        print "\n| by:lostwolf & 载入成功! |";
	print "\n| 去除重复，消除空白，冗余 ，无效数据中 |";
        echo "\n|". " 新的字典文件=>".dirname( __FILE__ ).DIRECTORY_SEPARATOR."new_file.txt |";
	print "\n+-----------------------------------------------------+\n\n\n";
	$num=count($file);
	echo "原始字典数量:" .$num."行"."\n";
	$new_num=count($new_array1);
	echo "整理后为:"."$new_num"."行"."\n";
	$counts=$num-$new_num;
	echo "共替换了".$counts."行"."\n\n";

}

else{   
	echo "------------------------------------------"."\r\n";
	echo "错误!"."\r\n";
	echo "找不到文件!请检查是否存在该字典!"."\r\n";
	echo "------------------------------------------"."\r\n";
}
 

?>

```

[hi.baidu.com/nginxshell](http://hi.baidu.com/nginxshell)

---

<div class="post-metadata">

作者： ![user22](https://forum.90sec.com/user_avatar/forum.90sec.com/user22/32/11361_2.png) [@user22](https://forum.90sec.com/u/user22)\
发布日期： [2011 年11 月 22 日 00:08 UTC](https://forum.90sec.com/t/topic/2948/2 "2011-11-22T00:08:37Z")

</div>

额 顶个，就不复制了。已经有别的工具代替{:4\_56:}

---

<div class="post-metadata">

作者： ![debugz3r0](https://forum.90sec.com/letter_avatar_proxy/v4/letter/d/41988e/32.png) [@debugz3r0](https://forum.90sec.com/u/debugz3r0)\
发布日期： [2012 年1 月 21 日 13:51 UTC](https://forum.90sec.com/t/topic/2948/3 "2012-01-21T13:51:13Z")

</div>

nice，网上那些整理出来的确实很多没什么用
