php删除txt文件指定行及按行读取txt文档数据的方法

向txt文件循环写入值:

  1. $keys= range(1,999);
  2. $file=<a href="/tags.php/fopen/" target="_blank">fopen</a>('key_11010000.txt',"w");
  3. <a href="/tags.php/foreach/" target="_blank">foreach</a>($keysas$key){
  4. fwrite($file,"$key\r\n");
  5. }
  6. fclose($file);
  7. $f1=fopen('key_11010000.txt','r');
  8. while(!feof($f1)){
  9. $line=fgets($f1);
  10. $line= trim($line);
  11. $arr[] =$line;
  12. }
  13. $keys=array_filter($arr);
  14. var_dump($keys);

按行读取txt文档数据:

  1. $f1=fopen('key_11010000.txt','r');
  2. while(!feof($f1)){
  3. $line=fgets($f1);
  4. $line= trim($line);
  5. $arr[] =$line;
  6. }
  7. $keys=array_filter($arr);
  8. var_dump($keys);
  9. die;