PHP文件操作 - 替换某行,插入某行,删除某行,获取行号
2017-03-01 13:56
627 查看
#在需要查找的内容后一行新起一行插入内容, #$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 9c6d ), 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); }
相关文章推荐
- PHP文件操作 - 替换某行,插入某行,删除某行,获取行号
- PHP文件操作之,插入某行,删除某行,获取行号
- PHP文件操作之,插入某行,删除某行,获取行号
- vb6 对文件的写入、替换、删除和插入某行操作
- php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)
- php文件操作小结(删除指定文件/获取文件夹下的文件名/读取文件夹下图片名)
- 11.21课堂笔记-查找节点和创建节点、插入节点和删除节点、复制节点替换节点、包裹节点和属性操作、样式操作、设置和获取HTML、文本和值
- [导入]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- php目录操作函数之获取目录与文件的类型
- ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)
- 在aspx文件里写C#代码(各种插入,选择,删除等操作,重要)
- php 获取目录下文件 批量改名 批量插入数据库
- [转]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- php目录、文件 遍历 删除 创建 操作 (php教程四)
- 插入替换删除文本文件中的某一行
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- StrinBuilder 追加 ,插入,删除,和替换操作
- C#代码对目录操作新建,删除 获取目录下文件列表等