簡體   English   中英

如何用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文件中的一行

您可以使用str_replace

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

由於文件系統的工作方式,您無法以直觀的方式執行此操作。 您必須使用除要刪除的行之外的所有行覆蓋文件,這是一個示例:

 $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

還有一種方法可以就地執行此操作以最小化所需的額外磁盤,但這更棘手。

您可以以編程方式執行此操作,它只會查看每一行,如果它不是您想要刪除的內容,則會將其推送到一個數組,該數組將被寫回文件。 像下面

 $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); 

首先使用fopenfget讀取文件,並制作數組以列出您要刪除的電子郵件,使用in_array檢查數組中是否存在值,然后在刪除不需要的電子郵件后使用fwrite保存文件,之后您需要關閉文件使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM