打印本文 打印本文  关闭窗口 关闭窗口
搜索和替换文件或目录的一个好类--很实用
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005/9/10 14:35:53
reak;

                                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;

                }

     

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



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