PHP文件操作
来源:互联网 发布:如何卸载linux系统 编辑:程序博客网 时间:2024/06/03 19:06
#在需要查找的内容后一行新起一行插入内容, #$position # start表示在第一次出现的位置插入, # end表示在最后一次出现的位置插入 function insertAfterTargetNewLine($filePath, $insertCont, $target,$position = 'start') { $result = null; $count = 1; $targetIndex = null; $fileCont = file_get_contents($filePath); if('start' == $position){//第一次出现的地方传入 $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标 } else if("end" == $position){//最后一次出现的地方传入 $targetIndex = strrpos($fileCont, $target); #查找目标字符串的坐标 } #找到target的后一个换行符 $chLineIndex = strpos(substr($fileCont, $targetIndex), PHP_EOL) + $targetIndex; if ($chLineIndex !== false) { #插入需要插入的内容 $fileCont = substr($fileCont, 0, $chLineIndex + 1) . $insertCont . PHP_EOL . substr($fileCont, $chLineIndex + 1); } file_put_contents($filePath, $fileCont); } #在需要查找的内容后插入内容 function insertAfterTarget($filePath, $insertCont, $target) { $result = null; $fileCont = file_get_contents($filePath); $count = substr_count($fileCont,$target);//查询子串出现的次数 echo $count; if($count > 0){ for($i=0;$i<$count;$i++){ $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标 if ($targetIndex !== false) { #插入需要插入的内容 $result = substr_replace($fileCont,$insertCont,$targetIndex+strlen($target),0);//起始位置,0表示插入 file_put_contents($filePath, $result); } } file_put_contents($filePath, $fileCont); }else{ return false; } } #替换指定字符串,或删除指定字符串 function replaceTarget($filePath,$target,$replaceCont='') { $result = null; $fileCont = file_get_contents($filePath); $count = substr_count($fileCont,$target);//查询子串出现的次数 echo $count; if($count > 0){ for($i=0;$i<$count;$i++){ $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标 if ($targetIndex !== false) { #替换指定行 $fileCont = substr_replace($fileCont,$replaceCont,$targetIndex,strlen($target)); } } file_put_contents($filePath, $fileCont); }else{ return false; } } #删除内容所在的某一行 function delTargetLine($filePath, $target) { $result = null; $fileCont = file_get_contents($filePath); $count = substr_count($fileCont,$target);//查询子串出现的次数 echo $count; if($count > 0){ for($i=0;$i<$count;$i++){ $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标 if ($targetIndex !== false) { #找到target的前一个换行符 $preChLineIndex = strrpos(substr($fileCont, 0, $targetIndex + 1), PHP_EOL); #找到target的后一个换行符 $AfterChLineIndex = strpos(substr($fileCont, $targetIndex), PHP_EOL) + $targetIndex; if ($preChLineIndex !== false && $AfterChLineIndex !== false) { #重新写入删掉指定行后的内容 $fileCont = substr($fileCont, 0, $preChLineIndex + 1) . substr($fileCont, $AfterChLineIndex + 1); } } } file_put_contents($filePath, $fileCont); }else{ return false; } }#获取某段内容的行号 /** * @param $filePath * @param $target 待查找字段 * @param bool $first 是否再匹配到第一个字段后退出 * @return array */ function getLineNum($filePath, $target, $first = false) { $fp = fopen($filePath, "r"); $lineNumArr = array(); $lineNum = 0; while (!feof($fp)) { $lineNum++; $lineCont = fgets($fp); if (strstr($lineCont, $target)) { if($first) { return $lineNum; } else { $lineNumArr[] = $lineNum; } } } return $lineNumArr; } public function index() { $info=[];// $result = $this->getNetworkInfo("cat /etc/resolv.conf"); $target = "127.1.1.1 www"; $filePath = "/etc/hostsbk"; // 文件结尾追加字符串// echo file_put_contents("/etc/hostsbk",$target.PHP_EOL,FILE_APPEND|LOCK_EX);// 删除字符串 $result = $this->delTargetLine($filePath,$target); dump($result); }
0 0
- php操作php文件
- 【php】目录操作 文件操作
- PHP操作文件问答
- PHP文件操作类
- PHP操作文件问答
- php文件操作
- PHP文件操作
- php写文件操作
- php文件操作
- PHP 文件读写操作
- php 操作csv文件
- PHP文件操作
- PHP读写文件操作
- php文件上传操作
- php 文件操作
- php 文件操作1
- php 文件操作
- PHP学习文件操作
- 蓝桥杯 ALGO-64 算法训练 大小写判断
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Centos6.7下yum 安装Mysql,以及配置PHP支持MySQL
- Spring mvc 接收到出不了的数据错误
- 根据身份证判断性别
- PHP文件操作
- oracle常用到的函数
- 通过趣味故事来认识kerberos(转)
- linux 查找 目录下的所有文件是否含有某个字符串
- java 基础知识(java 反射 用法总结)
- Git远程仓库地址变更本地如何修改
- 自定义view让图片慢慢画出来
- DAG
- 彻底释放Linux线程的资源