首页 > 开发 > PHP > 正文

全文搜索和替换

2024-05-04 23:00:41
字体:
来源:转载
供稿:网友
<?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  
注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表