全文搜索和替换
程序员文章站
2022-04-05 23:53:41
...
exec("/bin/grep -r '$oldWord' $rootpath", $results, $errorCode);
if ($errorCode){
if ($errorCode == 1){
echo "Possibly no files were found with $oldword in them
\n";
}
echo "OS Error: $errorCode
\n";
echo "Check 'man errno' and count down
\n";
echo "Usually paths/permissions
\n";
}
while (list(,$path) = each($results)){
$parts = explode(':', $path);
$path = $parts[0];
$fp = fopen($path, 'r') or PRint("Cannot read $path
\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
\n");
if ($fp){
fwrite($fp, $newdata);
fclose($fp);
echo $path, "
\n";
}
}
}
?>
Example
http://yourserver.com/globalreplace.php?oldword=test&newword=text&rootpath=/path/to/dir
if ($errorCode){
if ($errorCode == 1){
echo "Possibly no files were found with $oldword in them
\n";
}
echo "OS Error: $errorCode
\n";
echo "Check 'man errno' and count down
\n";
echo "Usually paths/permissions
\n";
}
while (list(,$path) = each($results)){
$parts = explode(':', $path);
$path = $parts[0];
$fp = fopen($path, 'r') or PRint("Cannot read $path
\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
\n");
if ($fp){
fwrite($fp, $newdata);
fclose($fp);
echo $path, "
\n";
}
}
}
?>
Example
http://yourserver.com/globalreplace.php?oldword=test&newword=text&rootpath=/path/to/dir
推荐阅读
-
lucene 基于ftp等其他文件系统创建索引和搜索索引的支持 博客分类: 原创 Lucenecommos-vfscommon-netftp
-
Oracle全文搜索中文
-
php 全文搜索和替换的实现代码_php技巧
-
shtml和html的区别有哪些,shtml更利于搜索引擎优化吗?
-
ASP.NET Core Web 应用程序系列(三)- 在ASP.NET Core中使用Autofac替换自带DI进行构造函数和属性的批量依赖注入(MVC当中应用)
-
讲解WordPress中用于获取评论模板和搜索表单的PHP函数
-
Notepad++的正则表达式替换和替换
-
java中的replaceAll()将字符串中的大括号和里面的参数替换掉
-
怎么用SELECT下拉菜单和TEXT文本框从选择数据库搜索数据
-
全新版大学英语综合教程2课文翻译 全文搜索和替换