Home > Backend Development > PHP Tutorial > 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

高洛峰
Release: 2023-03-05 10:54:01
Original
2608 people have browsed it

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);
Copy after login

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template