打印本文 打印本文  关闭窗口 关闭窗口
搜索和替换文件或目录的一个好类--很实用
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:35:53
;   //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<count($this->files); $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<count($this->directories); $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);
&nb

上一页  [1] [2] [3] [4]  下一页



打印本文 打印本文  关闭窗口 关闭窗口