"Cem Louis" <ce******@phreaker.net> wrote in message
news:12*************************@posting.google.co m...
Hi,
I want to get each line of e-mail accounts from my txt file and put
them in an array after checking the syntax.
I made a few tries but can't accomplish on that here is my text file
look like:
mails.txt
he@yourdomain.com
sh*@yourdomain.com
me@yourdomain.com
yo*@yourdomain.com
And here is my code:
<?
$filename = file("mails.txt");
foreach($filename as $line){
$syntax_ok_emails =
preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);
if($syntax_ok_emails){
$e_mails[] = $syntax_ok_emails;
}
}
foreach($e_mails[] as $value){
echo $value;
}
?>
Your preg_match isn't matching ANY of the email examples you gave and so the
$e_mails array isn't being loaded with any of addresses. I got a warning
when running your code, in this case it's because $e_mails isn't even set,
nevermind an array.
I'm sure there are hundreds upon hundreds of example checking solutions and
there are also hundreds of people that will tell you why it's wrong to do it
this way or that. Here's my interpretation of what you want to do though you
don't really need to be loading another array with the checked emails, you
could just output those that come true from the eregi (my example), unless
you're planning to do something with that array of course.
Anyhoo, my code:
<?
$filename = file("mails.txt");
foreach($filename as $address){
$address = trim($address);
if (eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',
$address)) {
$e_mails[] = $address;
}
}
foreach ($e_mails as $key => $value) {
echo $value."<br>";
}
?>
HTH
LJR