- <?php
- /**
- *遍历获取文件夹下所有文件中的中文字符
- *@author:firmy
- */
- $dir = $argv [1];
- if (empty ( $dir )) {
- help ();
- }
- /**
- * 遍历所有目录
- */
- $it = new RecursiveDirectoryIterator ( $dir );
- foreach ( new RecursiveIteratorIterator ( $it ) as $file ) {
- if (strpos ( $file, "svn" )) {
- continue;
- }
- getChineseWord ( $file );
- }
- /**
- * 获取文件中的中文字符
- *
- * @param unknown_type $file
- */
- function getChineseWord($file) {
- $x = file_get_contents ( $file );
- if (preg_match_all ( "/([\\x{4e00}-\\x{9fa5}\\x{fe30}-\\x{ffa0}]*)/u", $x, $match )) {
- foreach ( $match [0] as $k => $v ) {
- if (! empty ( $v )) {
- $wordList [$v] = $v; // 去重
- }
- }
- }
- }
- function help() {
- echo "
- ==============================================
- 请按以下参数运行
- php getChineseWord.php 文件夹 > 生成的文件名
- ==============================================
- ";
- die ();
- }
- //该片段来自于http://www.codesnippet.cn/detail/190820135203.html
来源: http://www.codesnippet.cn/detail/190820135203.html