찾다
백엔드 개발PHP 문제PHP에서 특정 줄을 삭제하는 방법

PHP에서 특정 줄을 삭제하는 방법: 먼저 PHP 샘플 파일을 만든 다음 delTargetLine 메서드를 정의한 다음 strpos 함수를 사용하여 대상 문자열의 좌표를 찾고 마지막으로 내용이 있는 특정 줄을 삭제합니다.

PHP에서 특정 줄을 삭제하는 방법

권장: "PHP 비디오 튜토리얼"

PHP 파일 작업, 특정 줄 삽입, 특정 줄 삭제, 줄 번호 가져오기

#在需要查找的内容后一行新起一行插入内容
     function insertAfterTarget($filePath, $insertCont, $target)
     {
         $result = null;
         $fileCont = file_get_contents($filePath);
          $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标
  
          if ($targetIndex !== false) {
              #找到target的后一个换行符
             $chLineIndex = strpos(substr($fileCont, $targetIndex), "\n") + $targetIndex;
             if ($chLineIndex !== false) {
                 #插入需要插入的内容
                 $result = substr($fileCont, 0, $chLineIndex + 1) . $insertCont . "\n" . substr($fileCont, $chLineIndex + 1);
                 $fp = fopen($filePath, "w+");
                 fwrite($fp, $result);
                 fclose($fp);
             }
         }
19     }
 
     #删除内容所在的某一行
     function delTargetLine($filePath, $target)
     {
         $result = null;
25         $fileCont = file_get_contents($filePath);
         $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标
 
         if ($targetIndex !== false) {
             #找到target的前一个换行符
             $preChLineIndex = strrpos(substr($fileCont, 0, $targetIndex + 1), "\n");
             #找到target的后一个换行符
             $AfterChLineIndex = strpos(substr($fileCont, $targetIndex), "\n") + $targetIndex;
             if ($preChLineIndex !== false && $AfterChLineIndex !== false) {
                 #重新写入删掉指定行后的内容
                 $result = substr($fileCont, 0, $preChLineIndex + 1) . substr($fileCont, $AfterChLineIndex + 1);
                 $fp = fopen($filePath, "w+");
                 fwrite($fp, $result);
                 fclose($fp);
             }
         }
     }
 
     #获取某段内容的行号
     /**
      * @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;
     }

위 내용은 PHP에서 특정 줄을 삭제하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

PhpStorm 맥 버전

PhpStorm 맥 버전

최신(2018.2.1) 전문 PHP 통합 개발 도구