如何使用 php 删除 txt 文件中的一行

how to delete a single line in a txt file with php

我想知道是否可以使用 php 删除 txt 文件中的一行。

我将电子邮件地址存储在一个名为 databse-email.txt

的平面 txt 文件中

我使用这个代码:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   $email = $_POST['email-subscribe'] . ',' . "\n";
   $store = file_put_contents('database-email.txt', $email, FILE_APPEND | LOCK_EX);
   if($store === false) {
     die('There was an error writing to this file');
   }
   else {
     echo "$email successfully added!";
   }
}

?>

表格:

<form action="" method="POST">
   <input name="email-subscribe" type="text" />
   <input type="submit" name="submit" value="Subscribe">
</form>

文件内容如下所示:

janny@live.nl,
francis@live.nl,
harry@hotmail.com,
olga@live.nl,
annelore@mail.ru,
igor@gmx.de,
natasha@hotmail.com,
janny.verlinden@gmail.com,

所有行都,分隔

假设我只想删除电子邮件地址:igor@gmx.de

我该怎么做?

我想要实现的是退订表单并删除 .txt 文件中的一行

由于文件系统的工作方式,您无法以直观的方式执行此操作。你必须用除了你想删除的行之外的所有行覆盖文件,这是一个例子:

 $emailToRemove = "igor@gmx.de";
 $contents = file('database-email.txt'); //Read all lines
 $contents = array_filter($contents, function ($email) use ($emailToRemove) {
      return trim($email, " \n\r,") != $emailToRemove;
 }); // Filter out the matching email
 file_put_contents('database-email.txt', implode("\n", $contents)); // Write back

在文件不适合内存的情况下,这里有一个流式替代解决方案:

$emailToRemove = "igor@gmx.de";
$fh = fopen('database-email.txt', "r"); //Current file
$fout = fopen('database-email.txt.new', "w"); //New temporary file
while (($line = fgets($fh)) !== null) {
      if (trim($line," \n\r,") != $emailToRemove) {
         fwrite($fout, $line, strlen($line)); //Write to new file if needed
      } 
}
fclose($fh);
fclose($fout);

unlink('database-email.txt');  //Delete old file 
rename('database-email.txt.new', 'database-email.txt'); //New file is old file

还有一种方法可以就地执行此操作以最大程度地减少所需的额外磁盘,但这比较棘手。

您可以使用str_replace

$content = file_get_contents('database-email.txt');
$content = str_replace('igor@gmx.de,', '', $content);
file_put_contents('database-email.txt', $content);

您可以通过编程方式执行此操作,它只会查看每一行,如果不是您要删除的内容,则会将其推送到一个数组,该数组将被写回文件。喜欢下面

 $DELETE = "igor@gmx.de";

 $data = file("database-email.txt");

 $out = array();

 foreach($data as $line) {
     if(trim($line) != $DELETE) {
         $out[] = $line;
     }
 }

 $fp = fopen("database-email.txt", "w+");
 flock($fp, LOCK_EX);
 foreach($out as $line) {
     fwrite($fp, $line);
 }
 flock($fp, LOCK_UN);
 fclose($fp); 

首先使用fopen and fget , and make array to list the emails you want to remove , use in_array to check if value exists in array , and then after remove unwanted emails save the file using fwrite and you need to close the file after the read and the write operations using fclose

读取文件

检查此代码

$data = "";
$emailsToRemove = ["igor@gmx.de" , "janny@live.nl"];

//open to read
$f = fopen('databse-email.txt','r');
while ($line = fgets($f)) {
    $emailWithComma = $line . ",";

    //check if email marked to remove
    if(in_array($emailWithComma , $emailsToRemove))
        continue;

    $data = $data . $line;
}

fclose($f);


//open to write
$f = fopen('databse-email.txt','w');
fwrite($f, $data);
fclose($fh);

删除特殊词然后删除空白行试试这个:

$file = "file_name.txt";
$search_for = "example_for_remove";
$file_data = file_get_contents($file);
$pattern = "/$search_for/mi";
$file_data_after_remove_word = preg_replace($pattern, '', $file_data);
$file_data_after_remove_blank_line = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $file_data_after_remove_word);
file_put_contents($file,$file_data_after_remove_blank_line);