php 全文搜索和替换的实现代码
程序员文章站
2022-07-01 15:18:33
<?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";
}
}
}
?>
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";
}
}
}
?>
上一篇: 锅巴菜的做法,怎么做好吃
下一篇: 咖啡人参能放在一起饮用吗?别再乱喝乱补了