您的位置:首页 > 编程语言 > PHP开发

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐