这是个非常有用的程序,可以对文本文件进行特定的搜索,并以特定的文字替换指定的文字,举个例子说如果我这篇文章里有一个字全部打错了,有几十处,要一一找出来修改是件很麻烦的事,用下面这个就可以轻松搞定。--teaman.oso.com.cn
类文件 search_replace.inc
class search_replace{
var $find;
var $replace;
var $files;
var $directories;
var $include_subdir;
var $ignore_lines;
var $ignore_sep;
var $occurences;
var $search_function;
var $last_error;
//以下进行函数定义和设置
function search_replace($find, $replace, $files, $directories = '', $include_subdir = 1, $ignore_lines = array()){
$this->find = $find;
$this->replace = $replace;
$this->files = $files;
$this->directories = $directories;
$this->include_subdir = $include_subdir;
$this->ignore_lines = $ignore_lines;
$this->occurences = 0;
$this->search_function = 'search';
$this->last_error = '';
}
/***************************************
** Accessor for retrieving occurences.
***************************************/
function get_num_occurences(){
return $this->occurences;
}
//获取最后的错误
function get_last_error(){
return $this->last_error;
}
//设置FIND变量
function set_find($find){
$this->find = $find;
}
//设置replace变量
function set_replace($replace){
$this->replace = $replace;
}
//设置FILE变量
function set_files($files){
$this->files = $files;
}
//设置目录变量
function set_directories($directories){
$this->directories = $directories;
}
//设置目录变量 set_include_subdir
function set_include_subdir($include_subdir){
$this->include_subdir = $include_subdir;
}
//设置ignore_lines变量
function set_ignore_lines($ignore_lines){
$this->ignore_lines = $ignore_lines;
}
//确定是哪一种搜索方式
function set_search_function($search_function){
switch($search_function){
case 'normal': $this->search_function = 'search';
return TRUE;
break;
case 'quick' : $this->search_function = 'quick_search';
return TRUE;
break;
case 'preg' : $this->search_function = 'preg_search';
return TRUE;
break;
case 'ereg' : $this->search_function = 'ereg_search';
return TRUE;
break;
default : $this->last_error = 'Invalid search function specified';
return FALSE;
break;
}
}
//以下为搜索和替换程序的主文件
function search($filename){
$occurences = 0;
$file_array = file($filename);
for($i=0; $i
if(count($this->ignore_lines) > 0){
for($j=0; $j
if(substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) $continue_flag = 1;
}
}
if($continue_flag == 1) continue;
$occurences = count(explode($this->find, $file_array[$i])) - 1;
$file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
}
if($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
return $return;
}
//使用quick(快速)搜索方法时,没有igonre_lines功能
function quick_search($filename){
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count(explode($this->find, $file)) - 1;
$file = str_replace($this->find, $this->replace, $file);
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
//preg搜索方法不支持ignore_lines
function preg_search($filename){
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count($matches = preg_split($this->find, $file)) - 1;
$file = preg_replace($this->find, $this->replace, $file);
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
//ereg搜索方法也不支持ignore_lines
function ereg_search($filename){
clearstatcache();
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$occurences = count($matches = split($this->find, $file)) -1;
$file = ereg_replace($this->find, $this->replace, $file);
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
//写新文件
function writeout($filename, $contents){
if($fp = @fopen($filename, 'w')){
fwrite($fp, $contents);
fclose($fp);
}else{
$this->last_error = 'Could not open file: '.$filename;
}
}
//由do_search调用,排出所有要搜索的文件
function do_files($ser_func){
if(!is_array($this->files)) $this->files = explode(',', $this->files);
for($i=0; $i
if($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
if(is_dir($this->files[$i]) == TRUE) continue;
$newfile = $this->$ser_func($this->files[$i]);
if(is_array($newfile) == TRUE){
$this->writeout($this->files[$i], $newfile[1]);
$this->occurences = $newfile[0];
}
}
}
//由do_search()调用,排出所有要搜索的目录
function do_directories($ser_func){
if(!is_array($this->directories)) $this->directories = explode(',', $this->directories);
for($i=0; $i
$dh = opendir($this->directories[$i]);
while($file = readdir($dh)){
if($file == '.' OR $file == '..') continue;
if(is_dir($this->directories[$i].$file) == TRUE){
if($this->include_subdir == 1){
$this->directories[] = $this->directories[$i].$file.'/';
continue;
}else{
continue;
}
}
$newfile = $this->$ser_func($this->directories[$i].$file);
if(is_array($newfile) == TRUE){
$this->writeout($this->directories[$i].$file, $newfile[1]);
$this->occurences = $newfile[0];
}
}
}
}
//调用这个do_search()就可以开始对文件或目录进行搜索
function do_search(){
if($this->find != ''){
if((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->do_files($this->search_function);
if($this->directories != '') $this->do_directories($this->search_function);
}
}
} // End of class
?>
//下面是调用该类的例子说明,请存为example.php
include('search_replace.inc'); //将文件包括进来
//建立新物件,设置搜索条件、最后返回搜索结果
$sr = new search_replace('asp', 'php', array('test.txt')); //调用搜索与替换
$sr->set_search_function('quick'); //设置搜索条件
$sr->do_search();
$sr->set_find('another');
$sr->do_search();
//下面是定制的返回信息
header('Content-Type: text/plain');
echo '发现和替换以下几个地方: '.$sr->get_num_occurences()."\r\n";
echo '啊,错误发生如下.............: '.$sr->get_last_error()."\r\n";
?>
//将以下文字存为test.txt,注意text.txt必须是可读可写的
"我非常喜欢asp,它简单易学,功能强,听说asp已经占了大半市场,asp真好。"
此时,如果您打开exampe.php 就会出现下面这些:
发现和替换以下几个地方:3
啊,错误发生如下..........:
查看test.txt文件,果然出现asp的地方被php替换了。

无论是文章、论文还是教程,任何文档的主要亮点都是标题,当然还有目录。它描述了文档的大纲结构,以便用户可以到达他们期待从文档中读取的位置和内容。将目录添加到大多数文档中以使它们看起来更专业也是一种最佳实践。如今,一切都在网上进行,人们使用谷歌文档来创建大多数文档。许多用户仍然不确定如何在 google 文档中插入或添加目录。因此,我们提出了这篇文章来解释如何在谷歌文档中创建或插入目录。如何在 Google Docs 中插入目录第 1 步:点击此处访问 Google Docs 在线版。第 2 步:如

全角英文字母转换为半角形式的实用技巧在现代生活中,我们经常会接触到英文字母,在使用电脑、手机等设备时也经常需要输入英文字母。然而,有时候我们会遇到全角英文字母的情况,而我们需要使用的是半角形式。那么,如何将全角英文字母转换为半角形式呢?下面就为大家介绍一些实用的技巧。首先,全角英文字母和数字是指在输入法中占据一个全角位置的字符,而半角英文字母和数字则是占据一

PyCharm新手指南:删除项目的实用技巧PyCharm是一款功能强大的Python集成开发环境(IDE),在进行项目开发时,有时候需要删除项目或项目中的文件。本文将介绍在PyCharm中删除项目的实用技巧,并提供具体的代码示例帮助新手更好地理解和应用。1.删除项目删除项目意味着删除整个项目文件夹,这在我们需要清理或重建项目时非常有用。在PyCharm中删

使用java的File.isDirectory()函数判断文件是否存在且为目录类型在Java编程中,经常会遇到需要判断一个文件是否存在且为目录类型的情况。Java提供了File类来操作文件和目录,其中的isDirectory()函数可以帮助我们判断一个文件是否是目录类型。File.isDirectory()函数是File类中的一个方法,其作用是判断当前Fil

PHP中的glob()函数用于查找文件或目录,是一种强大的文件操作函数。它可以根据指定的模式匹配,返回文件或目录的路径。glob()函数的语法如下:glob(pattern,flags)其中,pattern表示要匹配的模式字符串,可以是一个通配符表达式,如*.txt(匹配以.txt结尾的文件),或者是具体的文件路径。flags是一个可选参数,用于控制函数

PHP函数介绍—rename():重命名文件或目录介绍:在PHP中,rename()函数用于重命名文件或目录。它提供了一种简单的方法来更改文件或目录的名称。无论是单个文件还是整个目录,都可以使用这个函数进行重命名操作。通过指定源文件或目录的名称以及目标名称,可以轻松地完成重命名过程。语法:boolrename(string$source,str

linux查看目录是否为空的方法:1、进入linux终端;2、通过执行“res=`ls -A $dir` if [ -z $res ];then echo "$dir ..."else echo "$dir ..."fi”方法判断目录是否为空即可。

在word中挑选目录的款式,操作完成就可以自动生成了。解析1进到电脑的word,点一下引入。2进去后,点一下文件目录。3接着挑选文件目录的款式。4操作完成,就可以看到文件目录自动生成了。补充:总结/注意事项文章的目录自动生成,其中包括一级标题、二级标题和三级标题,通常不超过三级标题。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

记事本++7.3.1
好用且免费的代码编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。