仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 872|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网站制作之搜刮和交换文件或目次的一个好类--很实...

[复制链接]
小妖女 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:05:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
怎么培养啊 别光说不练啊,好 ,比如新人入门自己步是配置环境,虽然现在都有很多的集成环境,但是真实的体验下配置环境还是会有很多帮助,不论是你以后工作还是在真实的linux下开发。   这是个十分有效的法式,可以对文本文件停止特定的搜刮,并以特定的文字交换指定的文字,举个例子说假如我这篇文章里有一个字全体打错了,有几十处,要逐一找出来修正是件很费事的事,用上面这个就能够轻松弄定。--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();

                        $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);
                                        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

<?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交换了。
说说这一个月左右的学习情况和心得吧!我个人认为,既然决定了去做一件事,那就要以认真的态度去对待!既然决定来学习了,那不管当初是抱着怎样的心态来到这个培训班的,都要让自己认真的投入到学习中。
海妖 该用户已被删除
沙发
发表于 2015-2-4 08:47:40 | 只看该作者
建议加几个专业的phper的群,当然啦需要说话的人多,一处一点问题能有人回答你的,当然啦要让人回答你的问题,平时就得躲在里面聊天,大家混熟啦,愿意回答你问题的人自然就多啦。
小女巫 该用户已被删除
板凳
发表于 2015-2-4 20:06:08 | 只看该作者
Ps:以上纯属原创,如有雷同,纯属巧合
只想知道 该用户已被删除
地板
发表于 2015-2-10 05:45:12 | 只看该作者
学好程序语言,多些才是王道,写两个小时代码的作用绝对超过看一天书,这个我是深有体会(顺便还能练打字速度)。
乐观 该用户已被删除
5#
发表于 2015-2-10 08:35:44 | 只看该作者
个人呢觉得,配wamp 最容易漏的一步就是忘了把$PHP$目录下的libmysql.dll拷贝到windows系统目录的system32目录下,还有重启apache。
第二个灵魂 该用户已被删除
6#
发表于 2015-3-4 09:45:07 | 只看该作者
首先我是坚决反对新手上来就用框架的,因为对底层的东西一点都不了解,造成知识上的真空,会对以后的发展不利。我的观点上手了解下框架就好,代码还是手写。当然啦如果是位别的编程语言的高手的话,这个就另当别论啦。
小魔女 该用户已被删除
7#
发表于 2015-3-6 14:41:34 | 只看该作者
先学习php和mysql,还有css(html语言很简单)我认为现在的效果比以前的方法好。
admin 该用户已被删除
8#
发表于 2015-3-10 19:39:56 | 只看该作者
使用zendstdio 写代码的的时候,把tab 的缩进设置成4个空格是很有必要的
不帅 该用户已被删除
9#
发表于 2015-3-17 09:10:50 | 只看该作者
做为1门年轻的语言,php一直很努力。
飘飘悠悠 该用户已被删除
10#
发表于 2015-3-20 03:10:58 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
冷月葬花魂 该用户已被删除
11#
发表于 2015-4-1 14:49:52 | 只看该作者
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
深爱那片海 该用户已被删除
12#
发表于 2015-4-3 05:15:25 | 只看该作者
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
13#
发表于 2015-4-13 09:19:02 | 只看该作者
做为1门年轻的语言,php一直很努力。
分手快乐 该用户已被删除
14#
发表于 2015-4-13 14:39:45 | 只看该作者
遇到出错的时候,我经常把错误信息直接复制到 google的搜索栏,一般情况都是能搜到结果的,不过有时候会搜出来一大片英文的出来,这时候就得过滤一下,吧中文的弄出来,挨着式方法。
蒙在股里 该用户已被删除
15#
发表于 2015-4-16 21:11:38 | 只看该作者
至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。
再见西城 该用户已被删除
16#
发表于 2015-5-2 03:11:17 | 只看该作者
在我安装pear包的时候老是提示,缺少某某文件,才发现 那群extension 的排列是应该有一点的顺序,而我安装的版本的排序不是正常的排序。没办法我只好把那群冒号加了上去,只留下我需要使用的扩展。
再现理想 该用户已被删除
17#
发表于 2015-5-5 23:24:57 | 只看该作者
基础有没有对学习php没有太大区别,关键是兴趣。
简单生活 该用户已被删除
18#
发表于 2015-6-9 02:21:48 | 只看该作者
使用 jquery 等js框架的时候,要随时注意浏览器的更新情况,不然很容易发生框架不能使用。
山那边是海 该用户已被删除
19#
发表于 2015-6-16 20:01:21 | 只看该作者
再就是混迹于论坛啦,咱们的phpchina的论坛就很强大,提出的问题一般都是有达人去解答的,以前的帖子也要多看看也能学到不少前辈们的经验。别的不错的论坛例如php100,javaeye也是很不错的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-20 23:38

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表