| 
       要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替换了,我们先看最基本的. 
strtr() 函数转换字符串中特定的字符。 
语法:strtr(string,from,to)或者strtr(string,array),代码如下: 
	
	- $addr=strtr($addr,"","aao");   
 
	- $trans=array("hello"=>"hi","hi"=>"hello");   
 
	- echo strtr("hi all, i said hello",$trans);   
 
	-  
 
	-  
 
	- echo strtr("hilla warld","ia","eo"); 
 
	-  
 
	- $arr = array("hello" => "hi", "world" => "earth"); 
 
	- echo strtr("hello world",$arr); 
 
	 
 
如果 from 和 to 的长度不同,则格式化为最短的长度,再看一个简单的函数,代码如下: 
	
	- function file_modify($search_contents, $replace_contents, $filename) 
 
	- { 
 
	-         $fp = file_get_contents($filename); 
 
	-         $new_fp = str_replace($search_contents, $replace_contents, $fp); 
 
	-         file_put_contents($filename, $new_fp); 
 
	- } 
 
	-  
 
	- file_modify('sdf hjhj', 'sdf_test hjhj_test', 'test10.html'); 
 
	 
 
或者直接用如下这个: 
	
	- preg_replace('|(<div class="body">)(^<]+)(</div>)|iSU', "${1}" . 替换后的内容 . "$3", $str); 
 
	 
 
上面我所说的所有问题都只会替换一个文件里面的,那么我想替换一个站点所有目录里面文件指定字符,那么我们来看下面这个函数,代码如下: 
	
	- <?php      
 
	- if (isset($_GET['dir'])){  
 
	-     $basedir=$_GET['dir'];      
 
	- }else{      
 
	-     $basedir = '.';      
 
	- }    
 
	- $auto = 1;      
 
	- checkdir($basedir);      
 
	- function checkdir($basedir){      
 
	-     if ($dh = opendir($basedir)) {      
 
	-         while (($file = readdir($dh)) !== false) {      
 
	-             if ($file != '.' && $file != '..'){      
 
	-                 if (!is_dir($basedir."/".$file)) {      
 
	-                     echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";      
 
	-                 }else{      
 
	-                     $dirname = $basedir."/".$file;      
 
	-                     checkdir($dirname);      
 
	-                 }      
 
	-             }      
 
	-         }      
 
	-         closedir($dh);      
 
	-     }      
 
	- }      
 
	- function checkBOM ($filename) {      
 
	-     global $auto;      
 
	-     $contents = file_get_contents($filename);      
 
	-     $charset[1] = substr($contents, 0, 1);      
 
	-     $charset[2] = substr($contents, 1, 1);      
 
	-     $charset[3] = substr($contents, 2, 1);      
 
	-     if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {      
 
	-         if ($auto == 1) {      
 
	-             $rest = substr($contents, 3);      
 
	-             rewrite ($filename, $rest);      
 
	-             return ("<font color=red>BOM found, automatically removed._<a href=http://www.vcphp.com>http://www.vcphp.com/nokia/c6/</a></font>");      
 
	-         } else {      
 
	-             return ("<font color=red>BOM found.</font>");      
 
	-         }      
 
	-     }      
 
	-     else return ("BOM Not Found.");      
 
	- }      
 
	- function rewrite ($filename, $data) {      
 
	-     $filenum = fopen($filename, "w");      
 
	-     flock($filenum, LOCK_EX);      
 
	-     fwrite($filenum, $data);      
 
	-     fclose($filenum);      
 
	- }      
 
	- ?>   
 
	 
 
这样我们只要运行就可以替换指定目录所所有文件的所有内容,这个还是特别方便的。 
      
      (责任编辑:admin) |