全文搜索和替换

来源:互联网 发布:紧身衣尺码选择知乎 编辑:程序博客网 时间:2024/04/28 23:31
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<?php  
exec("/bin/grep -r '$oldword' $rootpath", $results, $errorCode);  
if ($errorCode){  
if ($errorCode == 1){  
echo "Possibly no files were found with $oldword in them<BR>/n";  
}  
echo "OS Error: $errorCode<BR>/n";  
echo "Check 'man errno' and count down<BR>/n";  
echo "Usually paths/permissions<BR>/n";  
}  
while (list(,$path) = each($results)){  
$parts = explode(':', $path);  
$path = $parts[0];  
$fp = fopen($path, 'r') or print("Cannot read $path<BR>/n");  
if ($fp){  
$data = fread($fp, filesize($path));  
fclose($fp);  
$newdata = str_replace($oldword, $newword, $data);  
$fp = fopen($path, 'w') or print("Cannot write $path<BR>/n");  
if ($fp){  
fwrite($fp, $newdata);  
fclose($fp);  
echo $path, "<BR>/n";  
}  
}  
}  
?>  
Example  
http://yourserver.com/globalreplace.php?oldword=test&newword=text&rootpath=/path/to/dir  
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击