One per line...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

One per line...

Post 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
&lt;br&gt;No it does not match hahah@haha.com
&lt;br&gt;No it does not match meme@meme.com&lt;br&gt;
How do I fix this?

Thanks

Ben
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

not sure if it will fix it but with my post and get variables I always do

Code: Select all

<?php
$_POST['email'];
?>
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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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()...
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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>";
}

}
?>
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

simple text

Post 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.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Nope coming our of MYSQL DB... I entered it via a textarea.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post 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'
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Why explode the array when you can just use inarray() in an if statement.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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. ;)
Post Reply