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!
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:
<?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:
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.
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 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