[英]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);
首先使用fopen
和fget
读取文件,并制作数组以列出您要删除的电子邮件,使用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.