Sep
15
突然发现这代码我写过n遍了,真不爽。。。
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。
<?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";
}
}
}
?>
$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 。