搜索和替换文件或目录的一个好类--很实用_PHP
这是个非常有用的程序,可以对文本文件进行特定的搜索,并以特定的文字替换指定的文字,举个例子说如果我这篇文章里有一个字全部打错了,有几十处,要一一找出来修改是件很麻烦的事,用下面这个就可以轻松搞定。--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替换了。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在電腦中刪除或解壓縮資料夾,時有時會彈出提示對話框“錯誤0x80004005:未指定錯誤”,如果遇到這中情況應該怎麼解決呢?提示錯誤碼0x80004005的原因其實很多,但大部分因為病毒導致,我們可以重新註冊dll來解決問題,下面,小編給大夥講解0x80004005錯誤代碼處理經驗。有使用者在使用電腦時出現錯誤代碼0X80004005的提示,0x80004005錯誤主要是由於電腦沒有正確註冊某些動態連結庫文件,或電腦與Internet之間存在不允許的HTTPS連接防火牆所引起。那麼如何

全角英文字母轉換為半角形式的實用技巧在現代生活中,我們經常會接觸到英文字母,在使用電腦、手機等設備時也經常需要輸入英文字母。然而,有時候我們會遇到全角英文字母的情況,而我們需要使用的是半角形式。那麼,如何將全角英文字母轉換為半角形式呢?以下就為大家介紹一些實用的技巧。首先,全角英文字母和數字是指在輸入法中佔據一個全角位置的字符,而半角英文字母和數字則是佔據一

夸克網盤和百度網盤都是現在最常用的儲存文件的網盤軟體,如果想要將夸克網盤內的文件保存到百度網盤,要怎麼操作呢?本期小編整理了夸克網盤電腦端的檔案轉移到百度網盤的教學步驟,一起來看看是怎麼操作吧。 夸克網盤的檔案怎麼存到百度網盤?要將夸克網盤的文件轉移到百度網盤,首先需在夸克網盤下載所需文件,然後在百度網盤用戶端中選擇目標資料夾並開啟。接著,將夸克網盤中下載的檔案拖放到百度網盤用戶端開啟的資料夾中,或使用上傳功能將檔案新增至百度網盤。確保上傳完成後在百度網盤中查看檔案是否已成功轉移。這樣就

智能為主導的時代,辦公室軟體也普及開來,Wps表格由於它的靈活性被廣大的辦公室人員採用。在工作上要求我們不只是要學會簡單的表格製作和文字輸入,我們要掌握更多的操作技能,才能完成實際工作中的任務,有數據的報表,運用表格更方便更清楚更準確。今天我們帶給大家的課程是:wps表格找不到正在搜尋的資料,為什麼請檢查搜尋選項位置? 1.先選取Excel表格,雙擊開啟。然後在該介面中,選取所有的儲存格。 2、然後在該介面中,點選頂部工具列裡「檔案」裡的「編輯」選項。 3、其次在該介面中,點選頂部工具列裡的“

最近有很多網友問小編,hiberfil.sys是什麼文件? hiberfil.sys佔用了大量的C碟空間可以刪除嗎?小編可以告訴大家hiberfil.sys檔是可以刪除的。下面就來看看詳細的內容。 hiberfil.sys是Windows系統中的隱藏文件,也是系統休眠文件。通常儲存在C盤根目錄下,其大小與系統安裝記憶體大小相當。這個檔案在電腦休眠時被使用,其中包含了當前系統的記憶體數據,以便在恢復時快速恢復到先前的狀態。由於其大小與記憶體容量相等,因此它可能會佔用較大的硬碟空間。 hiber

手機淘寶app軟體內提供的商品好物非常多,隨時隨地想買就買,而且件件都是正品,每一件商品的價格標籤一清二楚,完全沒有任何的複雜操作,享受更加便捷的購物樂趣。隨心所欲自由搜尋選購,不同品類的商品板塊都是開放的,添加個人的收貨地址以及聯絡電話,方便快遞公司聯繫到你,實時查看最新的物流動態,那麼有些新人用戶第一次使用它,不知道如何搜尋商品,當然只需要在搜尋欄輸入關鍵字就能找到所有的商品結果,自由選購根本停不下來,現在小編在線詳細為手機淘寶用戶們帶來搜尋店鋪名的方法。 1.先打開手機淘寶app,

在Linux系統中,可以使用下列指令來查看日誌檔案的內容:tail指令:tail指令用來顯示日誌檔案的末尾內容。它是查看最新日誌資訊的常用命令。 tail[選項][檔案名稱]常用的選項包括:-n:指定要顯示的行數,預設為10行。 -f:即時監視文件內容,並在文件更新時自動顯示新的內容。範例:tail-n20logfile.txt#顯示logfile.txt檔案的最後20行內容tail-flogfile.txt#即時監視logfile.txt檔案的更新內容head指令:head指令用於顯示記錄檔的開頭

MySQL中.ibd檔案的作用詳解及相關注意事項MySQL是一種流行的關聯式資料庫管理系統,資料庫中的資料儲存在不同的檔案中。其中,.ibd檔案是InnoDB儲存引擎中的資料文件,用於儲存表格中的資料和索引。本文將對MySQL中.ibd檔案的作用進行詳細解析,並提供相關程式碼範例以幫助讀者更好地理解。一、.ibd檔的作用:儲存資料:.ibd檔是InnoDB存
