How to delete one line in a txt file using php

I was wondering if it is possible to delete one line in a txt file using php.

I store emailadresses in a flat txt file named databse-email.txt

I use the code for this:

<?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!";
   }
}

?>

the form:

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

The contents of the file are as follows:

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,

All lines are ,split.

Say I want to delete only emails: igor@gmx.de

How can i do this?

What I want to achieve is a unsubscribe form and deleting one line in a file .txt

+4
source share
4 answers

you can use str_replace

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

Due to how the file system works, you cannot do this intuitively. You must rewrite the file with all the lines except the ones you want to delete, here is an example:

 $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

, , .

+3

You can do this programmatically, which will simply look at each line, and if this is not what you want to delete, it gets into an array that will be written back to the file. As below

 $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); 
+1
source

first read the file using fopenand fgetand make an array to list the emails you want to delete, use in_arrayto check if the value exists in the array and then after deleting the unwanted emails save the file using fwriteand you need to close the file after read operations and records usingfclose

check this code

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

Source: https://habr.com/ru/post/1692845/