這是個非常有用的程序,可以對文本文件進行特定的搜索,並以特定的文字替換指定的文字,舉個例子說如果我這篇文章裡有一個字全部打錯了,有幾十個地方,要一一找出來修改是件很麻煩的事,用下面這個就可以輕鬆搞定。--teaman.oso.com.cn
類別檔案search_replace.inc
class search_replace{
var $replace;
var $files;
var var $ignore_lines;
var $ignore_sep;
var $search_function;
var $last_error;
//以下進行函數定義與設定
function search_replace($find, $replace, $11, $. {
$this- >find = $find;
$this->files $this->directories = $directories;
= $include_subdir;
$this-> $this->occurences = 0;
$this->last_error = ' ';
}
/** return $this->occurences; function get_last_error(){
}
//設定FIND變數
, $this->find = $find;
} $this->replace = $replace; function set_files($files){ = $files;
}
//設定目錄變數
$this->directories = $directories; function set_include_subdir ($include_subdir){
}
//設定ignore_lines變數
$this->ignore_lines = $ignore_lines ;
}
//確定是哪一種搜尋方式
switch($search_function){
🎜> 返回TRUE;
休息;
case 'quick' : $this->search_function = 'quick_search';
返回TRUE;
休息;
case 'preg' : $this- >search_function = 'preg_search';
返回TRUE;
休息;
case 'ereg' : $this->search_function = 'ereg_search';
返回TRUE;
休息;
default : $this->last_error = '指定的搜尋功能無效';
返回FALSE;🎜> 休息;
}
}
//以下為搜索和替換程序的主文件
function search($filename){
$occurrences = 0;
$file_array = file($filename);
for($i=0; $i
如果(substr($file_array[$i],0,strlen($this->ignore_lines[$j] ) == $this->ignore_lines[$j]) $continue_flag = 1;
}
if($continue_flag == 1) 續;
explode($this->find, $file_array[$i])) - 1;
替換, $file_array[$i]) ;
}
turn = array($occurrences, implode('', $file_array));否則$return = FALSE;
}
//使用quick(快速)搜尋方法時,無igonre_lines功能
clearstatcache();
'r'), filesize($filename)); fclose($fp);
$file = str_replace($ this->find, $this->replace, $file);
if($occurencesturn > 0) $.$. return $return;
}
//preg ){
clearstatcache(); = fread($fp = fopen($filename, 'r '), filesize($filename)); fclose($fp);
$file = preg_replace( $this->find, $this->replace, $file);
if($occurencesturn > 0) $.$. return $return ;
}
//ereg filename){
clearstatcache(); read($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$file = ereg_replace($this->find, $this->replace, $file);
🎜> return $return;
}
//寫入新檔案
if($fp = @fopen($filename, 'w')){
$內容);
fclose($fp);
}else{
$this->last_error = '無法開啟檔案: '.$filename;
}
,提交所有要搜尋的文件
function do_files($ser_func){
files)) $this ->檔= 爆炸(',', $this->檔);
for($i=0; $i
if(is_dir($this->files[UEUE]) ==
$this->writeout($this->files[$i], $newfile[1 ]);
}
}
//由do_search()調用,導出所有要搜尋的目錄
if(!is_array($this->directories)) $this->directories = explode( ',', $this->目錄);
for($i=00 $ $dh = opendir($this->directories [$i]);
if($file == '.' OR $file if( is_dir($this->directories[$i].$file) == TRUE){ if($this->include_subdir == 1){
$this->目錄[] = $this->目錄[$ i].$file.'/';
繼續;
} else {
繼續;
}
}
地($this->directories[$i].$file, $newfile[1]);
$this->occurences += $newfile[0];
}
🎜> }
//呼叫這個do_search() search(){
if($this->find != if ((is_array($this->files) AND count($this->files) > 0) OR $this->files != '') $this->do_files($this->search_function); directories != '') }
// 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();
//下面是客製化的回傳資訊
header('Content-Type: .$sr->get_num_occurences()."rn";
echo '啊,錯誤發生如下.............: '.$sr->get_last_error()."rn" ;
?>
//將以下文字存為test.txt,注意text.txt必須是可讀可寫的
"我很喜歡asp,它簡單易學,功能強,聽說asp已經佔了大半市場,asp真好。 ................:
查看test.txt文件,果然出現asp的地方被php取代了。
以上就介紹了 搜尋和替換文件或目錄的一個好類--很實用,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP在現代Web開發中仍然重要,尤其在內容管理和電子商務平台。 1)PHP擁有豐富的生態系統和強大框架支持,如Laravel和Symfony。 2)性能優化可通過OPcache和Nginx實現。 3)PHP8.0引入JIT編譯器,提升性能。 4)雲原生應用通過Docker和Kubernetes部署,提高靈活性和可擴展性。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。