赵新朋:新时代农民工

  • 首页
  • 日常备忘
  • 服务器类
  • 我的收藏
  • 未分类
  • 国外VPS
欢迎你来到猪窝!
  1. 首页
  2. PHP代码
  3. 正文

php批量替换某文件夹下文本内容

2014/06/02 1827点热度 0人点赞 0条评论

/home/wwwroot/abc.com/dl
文件夹里面的所有HTML文件中有一段代码:
<a href="/sa/" target='_blank'><img src="static/image/common/logo.png" alt="abc" border="0" /></a>
想把它替换掉成空

以下是php代码

上传到/home/wwwroot目录下

<?php
//替换前字符串
$t1 = "<a href=\"/sa/\" target='_blank'><img src=\"static/image/common/logo.png\" alt=\"abc\" border=\"0\" /></a>";
//替换后字符串
$t2 = "";
//需要替换的目录
$dir = "./abc.com/dl";
function listdir($dir)
{
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..') {continue;}
if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;}
foreach(listdirx("$dir/$value") as $value)
{
$result[]=$value;

}
}
return $result;
}
$dir = "./textdir";
$arrdir = listdir($dir);
$i=0;
$zid = count($arrdir)-1;
while($i<=$zid) {
$file = $arrdir[$i];
$str1 = file_get_contents($file);

$str2 = str_replace($t1, $t2, $str1);

$wpfo=fopen($file,"w+");
fputs($wpfo,$str2);
fclose($wpfo);
$i++;
echo $i."已修改成功";
}

?>

 

标签: php
最后更新:2014/09/01

Kratos

保持饥渴的专注,追求最佳的品质

点赞
< 上一篇
下一篇 >

文章评论

取消回复
分类
  • PHP代码
  • 国外VPS
  • 建站技术
  • 我的收藏
  • 日常备忘
  • 服务器类
  • 未分类
2022年 7月
一 二 三 四 五 六 日
 123
45678910
11121314151617
18192021222324
25262728293031
« 6月    
    链接表
    • 西安保洁公司哪家好

    COPYRIGHT © 2021 赵新朋:新时代农民工. ALL RIGHTS RESERVED.

    THEME KRATOS MADE BY VTROIS