全文搜索和替換
更新時間:2006年10月09日 00:00:00 作者:
<?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
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
相關(guān)文章
PHP stristr() 函數(shù)(不區(qū)分大小寫的字符串查找)
在PHP中,stristr() 函數(shù)用來在一個字符串中查找另一個字符串。2010-06-06
PHP 第二節(jié) 數(shù)據(jù)類型之數(shù)值型
編程語言的兩大功能是數(shù)據(jù)處理和流程控制;數(shù)據(jù)處理的基礎(chǔ)是數(shù)據(jù)類型和數(shù)據(jù)接口;流程控制是各種控制語句;程序的組織協(xié)調(diào)是各種編程范式;這一節(jié)先看下PHP有哪些基本的數(shù)據(jù)類型2012-04-04
PHP的mysqli_stmt_init()函數(shù)講解
今天小編就為大家分享一篇關(guān)于PHP的mysqli_stmt_init()函數(shù)講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01

