当前位置: 首页 > 图文教程 > 网络编程 > PHP > 全文搜索和替换

PHP
图书管理程序(三)
PHP脚本的10个技巧(1)
PHP脚本的10个技巧(2)
用PHP调用数据库的存贮过程!
给多个地址发邮件的类
用函数读出数据表内容放入二维数组
用文本文件制作留言板提示(上)
用文本文件制作留言板提示(下)
WIN98下Apache1.3.14+PHP4.0.4的安装
Email+URL的判断和自动转换函数
一个高ai的分页函数和一个url函数
PHP的ASP防火墙
PHP网上调查系统
转换中文日期的PHP程序
全文搜索和替换
教你如何把一篇文章按要求分段
一个用于mysql的数据库抽象层函数库
php&java(三)
php&java(一)
php&java(二)

PHP 中的 全文搜索和替换


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 55 ::
收藏到网摘: n/a

<?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