用于从逗号分隔列表中删除电子邮件地址的 PHP 脚本
我运行一个小型网站,我的用户要求我建立一个邮件列表。我找到了一个简单的免费脚本,可以将电子邮件地址添加到 CSV 格式的受保护文本文件 email.txt
中:
[email protected],[email protected],blah,blah,blah
该脚本运行完美。然而,当用户取消其列表订阅时,通过该文件手动删除电子邮件地址是很麻烦的。我需要创建一个简单的脚本来删除电子邮件地址。
我想要的只是一个简单的 PHP 脚本,它显示一个文本框,以便用户可以输入他们的电子邮件地址并单击“取消新闻通讯”按钮。该脚本应搜索文本文件,找到给定的电子邮件地址并删除它及其结尾的逗号。
例如,假设 email.txt
的内容为
[email protected],[email protected],[email protected]
如果我输入“[email protected]”到我想要的脚本显示的文本框中,我想要文件看起来像这样:
[email protected],[email protected]
更新:我尝试了这个代码:
<?php
function showForm() {
echo '
<form method="post" action="">
Email Address: <input type="text" name="email"> <br />
<input type="submit" value="Cancel Newsletter" name="submit">
</form>
';
}
$_POST['email']
$to_delete = 'email';
$file = array_flip(explode(",",file_get_contents("email.txt")));
unset($file[$to_delete]);
file_put_contents("email.txt",implode(",",array_flip($file));
if(!$file_put_contents) {
die('Error occured');
} else {
echo 'Your subscription has been cancelled. You will not receive any further emails from us.';
}
}
} else {
showForm();
}
?>
这个代码甚至不显示表单。
更新 2:
编写此脚本的另一种尝试:
<?php
$email = $_POST["email"];
$text = file_get_contents("email.txt");
$oldWord = "$email";
$newWord = "";
$text = str_replace($oldWord , $newWord , $text);
$fp = fopen('email.txt', 'w');
fwrite($fp, $text);
fclose($file);
?>
只要删除电子邮件地址,此方法就有效,但没有公告(回声)。我想让它说“该电子邮件未订阅”或“您已被删除”,具体取决于脚本是否成功在列表中找到 $email
并将其删除。
更新3 2011年12月31日:
我尝试了高级代码,但只得到了一个空白页,所以我回到了我的版本。这是我现在拥有的代码:
<html>
<body>
<form method="post" action="">
<p>Email Address: <input type="text" name="email"></p>
<input type="submit" value="Cancel Newsletter" name="submit">
</form>
<?php
$email = $_POST["email"];
$basetext = file_get_contents("email.txt");
$oldWord = "$email";
$newWord = "";
$text = str_replace($oldWord , $newWord , $basetext);
$str1 = $basetext;
// echo strlen($str1);
$str2 = $text;
// echo strlen($str2);
$fp = fopen('email.txt', 'w');
fwrite($fp, $text);
if ($str1 > $str2) { // This part handles the success/fail message
echo ("Success!");
} else {
echo ("Fail!");
}
fclose($file);
?>
</body>
</html>
这完美地工作。但是,它在加载页面时显示“失败”消息,而不是在按下提交按钮后触发加载时显示“失败”消息。
如果可能的话,我想保留原始代码,只是重新排列,以便它只显示“成功!”或“失败!”一旦它执行了代码。
我希望回显消息是页面上执行的最后一个脚本。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有什么理由不使用数据库吗?
这些比文本文件更加简单和高效......
但是如果你想使用文本文件......
基本上它的作用是用逗号进行爆炸,然后翻转数组,以便电子邮件是键(及其数字位置是值,但这并不重要),然后它会删除您要删除的电子邮件,最后重新组合文件。这样做的好处是,它还会删除您可能拥有的任何重复项。
您可以使用类似的方法添加电子邮件地址,只需将
unset
行更改为$file['[email protected]'] = -1;
(确保数字不与任何内容冲突,因为这会干扰数组翻转)。Is there any reason why you don't use a database?
These are just infinitely easier and more efficient than a text file...
But if you want to use a text file...
Basically what it does it explodes by the comma, then flips the array so that the emails are keys (and their numeric positions are values, but that doesn't matter), then it removes the email you want to delete and finally reassembles the file. The bonus of this is that it will also strip out any duplicates you may have.
You can use a similar method to add email addresses, just changing the
unset
line to$file['[email protected]'] = -1;
(to ensure the number doesn't conflict with anything, as that would interfere with the array flipping).这个答案最初是由OP附加到问题正文中的。
首先,我将表单移至
/cancel.html
并使用(我编写的
.html
只是为了演示,因为我的服务器配置为不使用页面扩展,并且 PHP 在.html
页面上进行解析。 )然后我将 PHP 代码放入页面
/cancel_submit.html
中,并移至另一组 PHP 括号中。
这意味着电子邮件地址通过 POST 发送到另一个页面,然后该页面执行从列表中实际删除电子邮件地址的操作,然后检查是否删除了地址以提供确认消息。
我还在
$oldword = "$email";
中添加了两个逗号,使其$oldword = ",$email,";
以便它只查找输入的文本电子邮件信箱(如果两边都有逗号)。这解决了有人提交一半电子邮件地址的情况。我还将
$newWord = "";
更改为$newWord = ",";
,这样,如果脚本删除两边都有逗号的电子邮件地址,则两个旁边的电子邮件地址不会用逗号分隔。这是我现在两个页面的代码:
cancel.html
cancel_submit.html
<前><代码>
$str2){
echo (“您已成功取消订阅我们的时事通讯......
您将不会再收到我们发送的任何电子邮件。”);
} 别的 {
echo(“您指定的电子邮件地址不在我们的邮件列表中。”);
}
?>
请等待重定向或点击此处。
编辑:
我做了一些改进。我添加:
到电子邮件添加脚本和电子邮件删除脚本。这将以任一形式输入的所有字符都转换为小写;以前,它不会删除与大列表不同的情况下输入的电子邮件。
这搞乱了确认消息命令,所以我将其更改为
This answer was originally appended to the question body by the OP.
First I moved the form to
/cancel.html
and used<form action="/cancel_submit.html">
.(Where I have written
.html
, it is just to demonstrate, as my server is configured to use no page extentions and also so that PHP is parsed on.html
pages.)Then I put the PHP code into the page
/cancel_submit.html
and movedto another set of PHP brackets.
This meant that the e-mail adddress was sent via POST to the other page, which then performed the actual removal of the e-mail address from the list and then checked to see if an address been removed to provide the comfirmation message.
I also added two commas to
$oldword = "$email";
to make it$oldword = ",$email,";
so that it only finds the text entered into the e-mail box if it has a comma on either side. This addresses the case where someone submits half of an e-mail address.I also changed
$newWord = "";
to$newWord = ",";
so that if the script removes an e-mail address with commas at each side, the two e-mail addresses that were next to it will not be separated by a comma.Here is the code I have for both pages now:
cancel.html
cancel_submit.html
EDIT:
I made a few improvements. I added:
to both the e-mail add script and the e-mail remove script. This converted all characters entered into either form to lowercase; previously, it wouldnt remove e-mails typed in a different case than the big list had.
This messed up the confirmation message command, so I changed it to
建议研究:
http://us.php.net/manual/en/function .explode.php
https://www.php.net/manual/en/ function.file-put-contents.php
编辑添加:
https://www.php.net/manual/en/ function.file-get-contents.php
如果您最终使用第 3 方服务,请不要向 Aweber 付费。前往 MailChimp。如果您的邮件列表不是那么大,他们有一个免费计划。
Suggested research:
http://us.php.net/manual/en/function.explode.php
https://www.php.net/manual/en/function.file-put-contents.php
edited to add:
https://www.php.net/manual/en/function.file-get-contents.php
If you end up with a 3rd party service, don't pay Aweber. Go for MailChimp. They've got a free plan if your mailing list isn't that big.
在您的示例中,您引用变量
$_POST['email']
而不分配或测试该值。此外,您可能想要清理这个变量。我看到的另一个问题是
$to_delete = 'email';
,您只是在查找“email”条目。您的
$file_put_contents
尚未分配。} else { showForm(); }
没有与 if 语句配对。In your sample you reference a variable
$_POST['email']
without assignment or testing the value. Additionally you may want to sanitize this variable.Another issue I saw was that
$to_delete = 'email';
, you are only looking for entries of 'email'.Your
$file_put_contents
is not being assigned.} else { showForm(); }
wasn't paired up with an if statement.如果我正确理解你的问题,这就是你想要实现的目标。
我知道这可以作为一项非常简单的任务来完成,但我不相信这种方法。此外,我认为任何与数据永久存储交互的东西都应该有某种轻度到中度的抽象形式。
我会以这种方式完成这项任务。
If I understand your question correctly, this is what you are attempting to achieve.
I know this can be done as a very simple task, but I don't trust that approach. Additionally, I think anything interacting with permanent storage of data should have some mild to moderate form of abstraction.
I would approach that task this way.