Home  >  Article  >  Backend Development  >  How to delete specified lines of txt file in php and read txt document data line by line

How to delete specified lines of txt file in php and read txt document data line by line

高洛峰
高洛峰Original
2017-02-03 15:15:252452browse

The example of this article describes the method of PHP deleting specified lines of txt files and reading txt document data line by line. Share it with everyone for your reference, the details are as follows:

Write values ​​to the txt file in a loop:

$keys = range(1,999);
$file = fopen('key_11010000.txt',"w");
foreach($keys as $key){
  fwrite($file,"$key\r\n");
}
fclose($file);
$f1 = fopen('key_11010000.txt','r');
while(!feof($f1)){
  $line=fgets($f1);
  $line = trim($line);
  $arr[] = $line;
}
$keys = array_filter($arr);
var_dump($keys);

Read the txt document data line by line :

$f1 = fopen('key_11010000.txt','r');
while(!feof($f1)){
  $line=fgets($f1);
  $line = trim($line);
  $arr[] = $line;
}
$keys = array_filter($arr);
var_dump($keys);
die;

I hope this article will be helpful to everyone in PHP programming.

For more related articles on methods of deleting specified lines of txt files and reading txt document data line by line in PHP, please pay attention to the PHP Chinese website!

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