|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
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交换了。
说说这一个月左右的学习情况和心得吧!我个人认为,既然决定了去做一件事,那就要以认真的态度去对待!既然决定来学习了,那不管当初是抱着怎样的心态来到这个培训班的,都要让自己认真的投入到学习中。 |
|