搜索和替换文件或目录的一个好类--很实用

转载 2008年05月01日 04:26:00
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
这是个非常有用的程序,可以对文本文件进行特定的搜索,并以特定的文字替换指定的文字,举个例子说如果我这篇文章里有一个字全部打错了,有几十处,要一一找出来修改是件很麻烦的事,用下面这个就可以轻松搞定。--teaman.oso.com.cn
类文件 search_replace.inc
<?php


        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<count($file_array); $i++){
                                $continue_flag = 0;
                                if(count($this->ignore_lines) > 0){
                                        for($j=0; $j<count($this->ignore_lines); $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(); 共2页: 上一页 1 [2] 下一页 <script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

SharePoint 搜索教程

-
  • 1970年01月01日 08:00

搜索和替换文件或目录

        class search_replace{                 var $find;                 var $replace;              ...
  • zhihong123
  • zhihong123
  • 2009-03-03 11:27:00
  • 215

用Go语言写了一个电脑搜索文件的小东西

package main import ( "bytes" "fmt" "os" "os/exec" "path/filepath" "sync" ) type jieguo stru...
  • fyxichen
  • fyxichen
  • 2015-06-02 13:39:00
  • 1205

vim 查找多个文件, 替换

1、多文件查找  1.1、grep         直接在vim中输入:grep abc * 这是直接调用unix下的grep命令  1.2、vimgrep         基本用法就是  ...
  • u013408061
  • u013408061
  • 2017-07-26 11:09:41
  • 679

关于深恶痛绝的file类的delete问题

import java.io.File; import java.util.Scanner; /** * 要求用户输入一个文件或目录名,并删除当前目录下的该文件或目录。 */ public cl...
  • Chopper_X
  • Chopper_X
  • 2018-03-27 16:51:48
  • 10

查找和替换目录中所有文件中的某个字符串

1、查找目录下包含某个字符串的文件 例:查找 dir 目录下所有包含 str 的文件。 grep -lr 'str' dir 2、vim替换单个文件中所有字符串方法 例:替...
  • mecho
  • mecho
  • 2015-02-28 16:49:51
  • 2359

grep不查找有些文件夹

使用 --exclude-dir 选项 语法 --exclude-dir=DIR Exclude directories matching the pattern DIR from r...
  • yxfabcdefg
  • yxfabcdefg
  • 2017-06-09 17:17:10
  • 541

【好物推荐】一些实用网站

寒假集训第一天被学长安利一发大礼包,有必要做些记录2333(There are more things than coding) 鸠摩搜书(https://www.jiumodiary.co...
  • SaltyFishLu
  • SaltyFishLu
  • 2018-01-21 19:24:43
  • 189

Linux中查找目录或文件中的内容总结

查找目录下的所有文件中是否含有某个字符串  find .|xargs grep -ri "IBM"  查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名  find .|xargs g...
  • u011784994
  • u011784994
  • 2016-09-28 12:38:44
  • 5541

转:eclipse 项目中搜索资源(类方法,文件名,文件中的字符串)

http://www.cnblogs.com/fangyukuan/archive/2010/04/05/1704852.html 效果很好 编辑相关快捷键 Eclipse的编辑功能非常强大...
  • zp357252539
  • zp357252539
  • 2016-08-02 21:53:02
  • 2117
收藏助手
不良信息举报
您举报文章:搜索和替换文件或目录的一个好类--很实用
举报原因:
原因补充:

(最多只允许输入30个字)