全文搜索和替换
<?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
相关推荐
yanweiqi 2020-06-25
molong0 2020-06-13
PasserbyX 2020-05-16
wenwentana 2020-02-21
明瞳 2020-01-10
kekenow 2019-10-22
lhc0 2019-07-11
繌子 2017-12-18
繌子 2014-12-10
随手一记 2013-08-13
exitzhang 2012-11-22
YEEHOLIC 2019-06-29
fanix 2019-06-28
zhengsj 2013-01-27
EdShao 2019-06-27
看上去很美 2019-06-27
PasserbyX 2019-06-26
especialjie 2010-03-08