全文搜索和替换

1   
2exec("/bin/grep -r '$oldword' $rootpath", $results, $errorCode);   
3if ($errorCode){   
4if ($errorCode == 1){   
5echo "Possibly no files were found with $oldword in them

<br/>

1\n";   
2}   
3echo "OS Error: $errorCode

<br/>

1\n";   
2echo "Check 'man errno' and count down

<br/>

1\n";   
2echo "Usually paths/permissions

<br/>

1\n";   
2}   
3while (list(,$path) = each($results)){   
4$parts = explode(':', $path);   
5$path = $parts[0];   
6$fp = fopen($path, 'r') or print("Cannot read $path

<br/>

1\n");   
2if ($fp){   
3$data = fread($fp, filesize($path));   
4fclose($fp);   
5$newdata = str_replace($oldword, $newword, $data);   
6$fp = fopen($path, 'w') or print("Cannot write $path

<br/>

1\n");   
2if ($fp){   
3fwrite($fp, $newdata);   
4fclose($fp);   
5echo $path, "

<br/>

1\n";   
2}   
3}   
4}   

Example
http://yourserver.com/globalreplace.php?oldword=test&amp;newword=text&amp;rootpath=/path/to/dir

Published At
Categories with Web编程
Tagged with
comments powered by Disqus