Page 1 of 1
One per line...
Posted: Mon Oct 11, 2004 3:13 am
by Mr Tech
Hi there!
I've got this email script that sends out newsletters. I've added a section where you can add emails you want to ban into a text box, one per line. Like this:
Code: Select all
test@test.com
test@test2.com
test@test3.com
etc...
I then want to add a section in my subscribe form where it checks whether that email is banned. This is my code to check:
Code: Select all
<?php
$ban_emails = explode("\n", $ban_emails);
for($i = 0; $i < count($ban_emails); $i++) {
$ban_emails[$i] = str_replace("\n", "", $ban_emails[$i]);
if ($_POST[email] == $ban_emails[$i]) {
echo "Yes it matches $ban_emails[$i]<br>";
} else {
echo "No it does not match $ban_emails[$i]<br>";
}
}
?>
I entered
test@test.com as the $_POST[email] and it doesn't seem to pick it up. It seems to put an /n after the $ban_emails[$i] or something becuase this is what it ouputs:
Code: Select all
No it does not match test@test.com
<br>No it does not match hahah@haha.com
<br>No it does not match meme@meme.com<br>
How do I fix this?
Thanks
Ben
Posted: Mon Oct 11, 2004 3:23 am
by phpScott
not sure if it will fix it but with my post and get variables I always do
put the variable name in ' '
I think it has to do with security somewher. I sure one of the posting gurus out there will correct me if I wrong.
Posted: Mon Oct 11, 2004 3:24 am
by m3mn0n
If you have error reporting levels to E_ALL it will stop execution and tell you something about an undefined constant if you don't quote your array variables.
If you're exploding by something, it no longer is apart of the array values.
[php_man]foreach[/php_man]() is so much easier to use for looping through an entire array.
And I recommend you switch over to that for better organization and clarity.
Also, check into [php_man]print_r[/php_man]() for debugging. It helps a lot to see just what is inside of any given array you're working with.
Posted: Mon Oct 11, 2004 3:38 am
by Mr Tech
HI Sami,
I tried
foreach ($ban_emails as $b) {
echo "$b<br>";
}
And it didn't work... Can you give a code example?
I noticed in the examples for foreach() they used array()...
Posted: Mon Oct 11, 2004 3:40 am
by Mr Tech
Just tried:
$ban_emails = str_replace("\n", ",", $ban_emails);
$ban_emails = explode(",", $ban_emails);
foreach ($ban_emails as $b) {
echo "$b<br>";
}
But gave same output.
test@test.com
<br>
hahah@haha.com
<br>
meme@meme.com<br>
Posted: Mon Oct 11, 2004 3:42 am
by m3mn0n
Sure.
Code: Select all
<?php
$emails = explode ("\n", $ban_emails);
foreach($emails as $email)
{
if ($email == trim ( $_POST['emailaddress'] ))
{
echo "Banned.<br>";
} else {
echo "Valid.<br>";
}
}
?>
Posted: Mon Oct 11, 2004 3:58 am
by Mr Tech
hey Sami
It still seems to keep adding the \n to it and is giving me Valid. for each even when I enter the correct one... Any more ideas?
Ben
simple text
Posted: Mon Oct 11, 2004 5:04 am
by phpScott
is your ban list saved as a simple text document ie created in notepad and save as a .txt ?
It sounds like something has been added to the end of your text document that might be causing the problem.
Posted: Mon Oct 11, 2004 5:41 am
by Mr Tech
Nope coming our of MYSQL DB... I entered it via a textarea.
Posted: Mon Oct 11, 2004 6:10 am
by phpScott
I seem to recall having a simillar problem with textarea's and having to was the data before entering it into the db along the lines of nl2br or some sort of regex expression to reformat the data.
'phpScott runs away screaming for not being able to remeber or having access to the code'
Posted: Mon Oct 11, 2004 6:59 am
by kettle_drum
Why explode the array when you can just use inarray() in an if statement.
Posted: Mon Oct 11, 2004 1:35 pm
by John Cartwright
kettle_drum wrote:Why explode the array when you can just use inarray() in an if statement.
Because it's a string until you explode it?
What I would do is not worry about having to seperate the emails, and simply store them each as their own row, to allow for easier updating, removing, adding emails from your database. This way you can also store information like date banned, their IP address, etc
Posted: Mon Oct 11, 2004 7:09 pm
by Mr Tech
hmm that might just work... I'll give it a go. Thanks Phenom.
But I'm still interested why that extra \n appears if anyone knows the answer.
Posted: Mon Oct 11, 2004 11:07 pm
by m3mn0n
kettle_drum wrote:Why explode the array when you can just use inarray() in an if statement.
You can't [php_man]explode[/php_man]() arrays. You [php_man]implode[/php_man]() arrays.
The variable placed in the [php_man]explode[/php_man]() function was a string.
It helps to read a bit more careful before giving advice/criticism.
