Sep 15

PHP 递归处理文件 不指定

felix021 @ 2009-9-15 12:34 [IT » 程序设计] 评论(0) , 引用(0) , 阅读(3764) | Via 本站原创 | |
突然发现这代码我写过n遍了,真不爽。。。

<?php

$dirname = "ooxx";
$pattern = 'google';
$replace = 'baidu';
$DontReplace = true;

replace_rec($dirname);

function replace_rec($dirname, $layer = 0){
    global $DontReplace;
    static $cnt = 0;
    $dir = scandir($dirname);
    natcasesort($dir);
    foreach($dir as $item){
        if($item == '.' || $item == '..') continue;
        $path = "$dirname/$item";
        if(is_dir($path)){
            replace_rec($path, $layer+1);
        }else if(is_file($path)){
            $str = file_get_contents($path);
            if(strpos($str, $pattern) === false) continue;
            if($DontReplace == false){
                $str = str_replace($pattern, $replace, $str);
                file_put_contents($path, $str);
            }  
            $cnt++;
            for($i = 0; $i < $layer; $i++) echo '  '; echo "$cnt: $path\n";
        }else{
            echo "UNKNOWN TYPE\n";
        }  
    }  
}
?>




欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]