如何使用php删除txt文件中的一行 [英] how to delete a single line in a txt file with php
本文介绍了如何使用php删除txt文件中的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以用php删除txt文件中的一行.
i was wondering if it is posible to delete a single line in a txt file with php.
我将电子邮件地址存储在名为databse-email.txt
I am storing emailadresses in a flat txt file named databse-email.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
Lets say i want to delete only the emailadres: igor@gmx.de
我该怎么做?
我要实现的是取消订阅表单并删除.txt
文件中的一行
What i want to achieve is a unsubscribe form and delete a single line in the .txt
file
推荐答案
您可以使用str_replace
$content = file_get_contents('database-email.txt');
$content = str_replace('igor@gmx.de,', '', $content);
file_put_contents('database-email.txt', $content);
这篇关于如何使用php删除txt文件中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文