An array question???

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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:


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]$/', $line);
        $e_mails[] = $syntax_ok_emails;
foreach($e_mails[] as $value){
    echo $value;

Re: An array question???

Quoted text here. Click to load it

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



Re: An array question???

Quoted text here. Click to load it

Oh, in addition to the above, I noticed in your code your last "foreach" was
the following:
foreach($e_mails[] as $value) {
    echo $value;

It should be:
foreach($e_mails as $value) {
    echo $value;

So, two problems:
1. preg_match isn't doing its job at all.
2. Wrong parameter in foreach()


Re: An array question???

 .oO(Cem Louis)

Quoted text here. Click to load it

There are several issues with your code:

* Use <?php instead of <?, the latter (short open tags) is less reliable
  and may be disabled on some servers

* file() returns the file content _including_ line breaks (that's one
  reson why the pattern matching fails)

* $e_mails is not initialized, the second foreach-loop causes a notice
  if no valid addresses were found

* There's a missing [ in the pattern in its second part (after the @)

* The pattern only matches a small subset of valid mail addresses, there
  are much more chars and structures allowed, but maybe it's enough in
  your case

* You're storing the boolean values instead of the mail addresses in the
  $e_mails array

Try this:

$e_mails = array();
$filename = file('mails.txt');
foreach ($filename as $line) {
  $line = trim($line);
  if (preg_match('#^[\w-.]+@([\w-]+\.)+\w$#', $line)) {
    $e_mails[] = $line;


Re: An array question???

Michael Fesser a écrit :
Quoted text here. Click to load it
Quoted text here. Click to load it
 > foreach ($filename as $line) {

And here I would add that $filename is obviously a wrong name for the
variable... $lines would probably do a better job :

$lines = files('mails.txt');
foreach ($lines as $lines) {

Quoted text here. Click to load it


Re: An array question???

Quoted text here. Click to load it

That would cause some grief. Rather "foreach ($lines as $line)" maybe. ;-)

 - Virgil

Re: An array question???

Cem Louis wrote:
Quoted text here. Click to load it

I am not a guru on regex, but accoriding to my layman judgement:

The - in the first [] does not need escaping.
The second [] misses a [
I tried your regex with these alterations and preg_match with my email addy
returns a 1, which is what you want ? I would say you need to store the
$line itself in a db, not the preg_match result.

Besides, I wonder if you caver all possible emailaddresses with this, I
dunno the exact standard for valid email... May have to recheck that


Site Timeline