PHP 批量更新网页内容实现代码
程序员文章站
2022-06-12 07:53:26
复制代码 代码如下:
<?php
$path=$document_root;
$path=str_replace("/","\\",$path);
//指定文件夹
$path=$path."\\web\\study\\";
//得到所有文件
$s=explode("\n",trim(`dir/b/o:gn $path`));
//得到文件夹下的所有html文件名
$num=count($s);
for($i=0;$i<=$num;$i++){
if(strpos($s[$i],".htm")==0){
unset($s[$i]);
}
}
//print_r($s);
//批处理网页
foreach($s as $value){
editfile("d:\\myoa\\webroot\\web\\study\\".$value);
echo $value."已更新!<br>";
}
//editfile("d:\\myoa\\webroot\\web\\study\\0105_5.htm");
echo "the end";
//自定义函数:截取标签之间的内容
function get_body($start_str,$end_str,$content){
$start_str_num=strlen($start_str);
$end_str_num=strlen($end_str);
$a=strpos($content,$start_str)+$start_str_num;
$b=strpos($content,$end_str)-strpos($content,$start_str)-$start_str_num;
return substr($content,$a,$b);
}
//编辑文件
function editfile($str){
//$str="d:\\myoa\\webroot\\web\\study\\".$str
//取得html内容
$file=@fopen($str,"r");
while(!feof($file)){
$result.=fgets($file,9999);
}
fclose($file);
//替换内容
$result=str_replace("老王的数码配件店:王婆数码","岚视界 ",$result);
$result=str_replace("摄影网校","回到教程首页",$result);
$result=str_replace('width="407"','width="770"',$result);
$result=str_replace("http://shop1289309.taobao.com","http://www.lansj.com",$result);
$result=str_replace("老王用的id有:pccity,joshwang,joshwanggg","",$result);
$result=str_replace("qq:123709080 msn:joshwanggg@hotmail.com",'在线咨询: <a target=blank href=tencent://message/?uin=35501547&site=岚视界摄影&menu=yes><img border="0" src=http://wpa.qq.com/pa?p=1:35501547:10 alt="点击开始咨询"></a> <a target=blank href=tencent://message/?uin=56025743&site=岚视界摄影&menu=yes><img border="0" src=http://wpa.qq.com/pa?p=1:56025743:10 alt="点击开始咨询"></a>',$result);
$result=str_replace('<a href="http://www.zonline.com.cn">http://www.zonline.com.cn</a><br>',"",$result);
$result=str_replace('http://www.zonline.com.cn/photo/school/scgs/',"http://www.lansj.com/study/",$result);
$result=str_replace('http://www.yimei.net/css/',"http://www.lansj.com/study/",$result);
$result=str_replace('http://www.zonline.com.cn/photo/school/img/',"http://www.lansj.com/study/",$result);
$result=str_replace('http://www.yimei.net/inc/',"http://www.lansj.com/study/",$result);
$result=str_replace('<a href="http://www.xiangshu.com">橡树摄影俱乐部</a>','<a href="http://bbs.lansj.com">岚视界摄影论坛</a>',$result);
$result=str_replace('http://www.zonline.com.cn/photo/img/','http://www.lansj.com/study/',$result);
$result=str_replace('<p>如果你对数码器材和配件有什么问题可以发qq:123709080问询,数码相机及相关配件专营:<a href="http;//shop1289309.taobao.com">王婆数码</a></p>',"",$result);
$result=str_replace("untitled document",substr(str_replace(chr(32),"",str_replace(chr(10),"",str_replace("</p>","",str_replace(chr(13),"",get_body('<p align="center" class="h1"><br>','<p align="left">',$result))))),0,28)." - 岚视界摄影",$result);
$result=str_replace("摄影天地",substr(str_replace(chr(32),"",str_replace(chr(10),"",str_replace("</p>","",str_replace(chr(13),"",get_body('<span class="text">','</span>',$result))))),0,28)." - 岚视界摄影",$result);
//echo $result;
//更新html
$file=@fopen($str,"w");
fwrite($file,$result);
fclose($file);
}
?>
复制代码 代码如下:
<?php
$path=$document_root;
$path=str_replace("/","\\",$path);
//指定文件夹
$path=$path."\\web\\study\\";
//得到所有文件
$s=explode("\n",trim(`dir/b/o:gn $path`));
//得到文件夹下的所有html文件名
$num=count($s);
for($i=0;$i<=$num;$i++){
if(strpos($s[$i],".htm")==0){
unset($s[$i]);
}
}
//print_r($s);
//批处理网页
foreach($s as $value){
editfile("d:\\myoa\\webroot\\web\\study\\".$value);
echo $value."已更新!<br>";
}
//editfile("d:\\myoa\\webroot\\web\\study\\0105_5.htm");
echo "the end";
//自定义函数:截取标签之间的内容
function get_body($start_str,$end_str,$content){
$start_str_num=strlen($start_str);
$end_str_num=strlen($end_str);
$a=strpos($content,$start_str)+$start_str_num;
$b=strpos($content,$end_str)-strpos($content,$start_str)-$start_str_num;
return substr($content,$a,$b);
}
//编辑文件
function editfile($str){
//$str="d:\\myoa\\webroot\\web\\study\\".$str
//取得html内容
$file=@fopen($str,"r");
while(!feof($file)){
$result.=fgets($file,9999);
}
fclose($file);
//替换内容
$result=str_replace("老王的数码配件店:王婆数码","岚视界 ",$result);
$result=str_replace("摄影网校","回到教程首页",$result);
$result=str_replace('width="407"','width="770"',$result);
$result=str_replace("http://shop1289309.taobao.com","http://www.lansj.com",$result);
$result=str_replace("老王用的id有:pccity,joshwang,joshwanggg","",$result);
$result=str_replace("qq:123709080 msn:joshwanggg@hotmail.com",'在线咨询: <a target=blank href=tencent://message/?uin=35501547&site=岚视界摄影&menu=yes><img border="0" src=http://wpa.qq.com/pa?p=1:35501547:10 alt="点击开始咨询"></a> <a target=blank href=tencent://message/?uin=56025743&site=岚视界摄影&menu=yes><img border="0" src=http://wpa.qq.com/pa?p=1:56025743:10 alt="点击开始咨询"></a>',$result);
$result=str_replace('<a href="http://www.zonline.com.cn">http://www.zonline.com.cn</a><br>',"",$result);
$result=str_replace('http://www.zonline.com.cn/photo/school/scgs/',"http://www.lansj.com/study/",$result);
$result=str_replace('http://www.yimei.net/css/',"http://www.lansj.com/study/",$result);
$result=str_replace('http://www.zonline.com.cn/photo/school/img/',"http://www.lansj.com/study/",$result);
$result=str_replace('http://www.yimei.net/inc/',"http://www.lansj.com/study/",$result);
$result=str_replace('<a href="http://www.xiangshu.com">橡树摄影俱乐部</a>','<a href="http://bbs.lansj.com">岚视界摄影论坛</a>',$result);
$result=str_replace('http://www.zonline.com.cn/photo/img/','http://www.lansj.com/study/',$result);
$result=str_replace('<p>如果你对数码器材和配件有什么问题可以发qq:123709080问询,数码相机及相关配件专营:<a href="http;//shop1289309.taobao.com">王婆数码</a></p>',"",$result);
$result=str_replace("untitled document",substr(str_replace(chr(32),"",str_replace(chr(10),"",str_replace("</p>","",str_replace(chr(13),"",get_body('<p align="center" class="h1"><br>','<p align="left">',$result))))),0,28)." - 岚视界摄影",$result);
$result=str_replace("摄影天地",substr(str_replace(chr(32),"",str_replace(chr(10),"",str_replace("</p>","",str_replace(chr(13),"",get_body('<span class="text">','</span>',$result))))),0,28)." - 岚视界摄影",$result);
//echo $result;
//更新html
$file=@fopen($str,"w");
fwrite($file,$result);
fclose($file);
}
?>
上一篇: 百度网盘批量处理助手使用教程
下一篇: php 无极分类(递归)实现代码