龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 遍历目录,获取文件中的中文

时间:2014-07-20 02:03来源:网络整理 作者:网络 点击:
分享到:
遍历目录,获取文件中的中文 [代码片段(48行)]
<?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://outofmemory.cn
精彩图集

赞助商链接