Home  >  Article  >  Backend Development  >  How to remove the first few lines of a file in php_php tips

How to remove the first few lines of a file in php_php tips

WBOY
WBOYOriginal
2016-05-16 20:09:451196browse

The example in this article describes how to remove the first few lines of a file in PHP. Share it with everyone for your reference. The details are as follows:

For example, remove the first few lines in the lrc file:

[ti:]
[ar:]
[al:]
[by:cww.99Lrc.net]
[00:00.59]dasfdasfasdf
[00:30.18]dafadsf

php implementation code is as follows:

<&#63;php
foreach (new DirectoryIterator('./') as $fileInfo) {
  if($fileInfo->isDot()) continue;
  if($fileInfo->getExtension()=="lrc"){
    $handle = fopen($fileInfo->getFilename(), 'rw');
    $line_num=0;
    while(!feof($handle)){
      $str=fgets($handle, 1024);
      if(strstr($str, "www.99Lrc.net")){
        ob_start();
        fpassthru($handle);
        //fclose($handle);
        file_put_contents($fileInfo->getFilename(),ob_get_clean());
        printf('done');
        break;
      }
      $line_num++;
    }
    fclose($handle);
    echo $fileInfo->getFilename() . PHP_EOL;
  }
}

I hope this article will be helpful to everyone’s PHP programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn