Forum Moderators: coopster
Here is an example of what I have so far;
$contactlist = $_POST['friends']; //value from text field
$contactlist = str_replace("\r\n", "\n", $contactlist);
$csv_array= explode(",",$contactlist);
$csvnum=count($csv_array);
for ($n=0;$n<$csvnum;$n++)
{
trim($csv_array[$n]);
$emails = $csv_array[$n];
if( !eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $emails)){
$badMail = "You've got an invalid email somewhere";
}
//end mail check loop
}
Can anyone tell me how to tell which particular email is invalid?
Thanks
$emails
you just need to change
$badMail = "You've got an invalid email somewhere";
to
$badMail = "You've got an invalid email: $emails";
-------------------
$contactlist = "example1@example.com,example2@example.com,example3example.com,example4@example.com";$contactlist = str_replace("\r\n", "\n", $contactlist);
$csv_array= explode(",",$contactlist);
$csvnum=count($csv_array);
for ($n=0;$n<$csvnum;$n++)
{
trim($csv_array[$n]);
$emails = $csv_array[$n];
if(!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $emails)){
$badMail = "You've got an invalid email: $emails";
echo $badMail . "\t";
}
//end mail check loop
}
outputs:
You've got an invalid email: example3example3.com
You've got an invalid email: example3example.com
If you run your code and you are seeing all emails then there must be a problem with the csv file (in my example I have hard coded the variable).
Try some debugging.
At the start of your code print out the csv variable to see if it is CSV:
******
$contactlist = $_POST['friends']; //value from text field
$contactlist = str_replace("\r\n", "\n", $contactlist);
$csv_array= explode(",",$contactlist);
$csvnum=count($csv_array);
******
below that add
echo $contactlist;
echo "\t" . $csvnum;
You should see all your email addresses comma separated and the total number of elements in the array.
If it doesn't look like csv and the csvnum is 1 then the problem is with the csv datafile.
[edited by: Frank_Rizzo at 10:26 am (utc) on May 6, 2009]
You've got an invalid email: example1example.com You've got an invalid email: example3example.com You've got an invalid email: example4@example.comexample1@example.com You've got an invalid email: example3example.com
example4@example.comexample1@example.com
If the csv file is not valid then it will be difficult for the script to determine the start and end of an email address.
How is the code entered? line by line?
example1@example.com,
example2@example.com,
or inline?
example1@example.com,example2@example.com,
You may need to parse the data in the first example to ensure that each line ends in a comma (I see you replace \r\n with \n) ...
Consider line by line without the commas and then replace them after they are posted
User posts:->
example1@example.com
example2@example.com
$contactlist = $_POST['friends']; //value from text field
$contactlist = str_replace("\r\n", ",", $contactlist);
$csv_array= explode(",",$contactlist);
(note that you may need to trim off the last ","
BTW I do not tend to use commas for csv data incase a user makes a typo by putting in a comma
e.g.
example1@example,com
The comma is next to the dot. Far better to use a character which needs shifting on the keyboard such as a broken pipe or tilde
example1@example.com¦example2@example.com
or
example1@example.com~example2@example.com
Using a different seperator will significantly reduce parsing errors.