PHP,替换多个文件中的文本
需求:
需要对两个目录中的文件内容进行查找替换,两个目录查找替换的内容不一样。
实现中注意几点:
(1)函数递归调用,来检索子目录;
(2)函数名可以以参数形式传递;
(3)用?进行非贪婪匹配;
(4)每行匹配查找、整个文件内容匹配查找
<?php
//loopdir('dir1', 'repfile1');
loopdir('dir2', 'repfile2');
function loopdir($dir, $fun){
$dp = opendir($dir);
chdir($dir);
readdir($dp); //排除当前目录 "."
readdir($dp); //排除上层目录 "."
while( $v = readdir($dp) ){
if( is_dir($v) ) loopdir($v, $fun);
else $fun($v);
echo $v."<br>";
}
chdir("..");
}
function repfile1($filename){
$content = file_get_contents($filename);
if( empty($content) ){
echo "can't open file to read: ".$filename;
return ;
}
//加?号是进行非贪婪匹配,以免匹配的块太大
$content = preg_replace('/if \(!validateEmail\((.*?)"Email address is not valid(.*?})/s',
'if (!validateEmail(\\1"Email address is not valid\\2else if (!isNyhEmail(\\1"Can not add NYH email addresses.\\2',
$content);
writenewfile($filename, $content);
}
function repfile4615($filename){
$fpr = fopen($filename, 'r');
if( empty($fpr) ){
echo "can't open file to read: ".$filename;
return ;
}
$newContent = '';
while( $line = fgets($fpr) ){
$line = preg_replace('/^(.*)\$var_kitchen_dishwasher = "kitchen".\$kitchen_ind."_dishwasher";/s',
'\\1$var_kitchen_dishwasher = "kitchen".\$kitchen_ind."_dishwasher";
\\1$var_kitchen_oven = "kitchen".$kitchen_ind."_oven";
\\1$var_kitchen_fridge = "kitchen".$kitchen_ind."_fridge";', $line);
$newContent .= $line;
}
fclose($fpr);
writenewfile($filename, $newContent);
}
function writenewfile($filename, $content){
$fpw = fopen($filename, 'w');
if( empty($fpw) ){
echo "can't open file to write: ".$filename;
return ;
}
fwrite($fpw, $content);
fclose($fpw);
}
?>
相关文档:
1.类和对象。
类是一类事物的描述。抽象,概念上的定义。
对象是实际存在的每个个体。也叫实例。对象是new出来的。$p = new Person();
2.属性
用来描述类中的数据元素。(也叫数据/状态)
修饰符(PHP5中必须修饰):public(公开,类的内外部读取修改),private(私有只在当前类中读取修改),protected(保护,能在这个类和 ......
返回一个结果全世界都知道怎么处理,关键是返回多个结果集就不好办了,下面有一解决办法
存储过程代码
DELIMITER $$;
DROP PROCEDURE IF EXISTS `test`.`sp_test`$$
CREATE PROCEDURE `test`.`sp_test` ()
BEGIN
select * from `user`.`user` limit 0, 50;
select coun ......
1,设计一个程序的流程
设计流程:
功能需要->页面设计->数据库设计->代码设计->调试发布.
开始程序->初始化->条件->结束;
2.heml表单标签的使用:
<form action = "" method = "get">
<input type="test" size=10 name="user"/>
<input type="test" size=10 name="title"& ......
这是我写的第一个计数器
<?
$s_ht="127.0.0.1";
$s_user="root";
$s_pas="123456";
$s_data="ceshi";
if (empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$myip = $_SERVER['REMOTE_ADDR'];
$myip = $_SERVER['REMOTE_ADDR'];
}
else {
$myip = explode(',', $_SERVER['HTTP_X_FORWA ......
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体" ......