By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,353 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,353 IT Pros & Developers. It's quick & easy.

An array question???

P: n/a
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;
}
?>
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
LJR
"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
Jul 17 '05 #2

P: n/a
LJR
"LJR" <i_would_but_i do************@no-hope.co.uk> wrote in message
news:41***********************@mercury.nildram.net ...
"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


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()

:)
Jul 17 '05 #3

P: n/a
.oO(Cem Louis)
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: [...]


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{2,4}$#', $line)) {
$e_mails[] = $line;
}
}
HTH
Micha
Jul 17 '05 #4

P: n/a
Cem Louis wrote:

he@yourdomain.com
sh*@yourdomain.com
me@yourdomain.com
yo*@yourdomain.com

$syntax_ok_emails =
preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);


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

HTH
Pjotr
Jul 17 '05 #5

P: n/a
On Sat, 07 Aug 2004 13:17:37 +0200, Pjotr Wedersteers wrote:
Cem Louis wrote:

he@yourdomain.com
sh*@yourdomain.com
me@yourdomain.com
yo*@yourdomain.com

$syntax_ok_emails =
preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);


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

HTH
Pjotr

Nope.. {2,4} for the TLD needs to be {2,6} at least (.museum is a
legitimate TLD), other issues also occur in the username part (no + or .
permitted by the above regex etc).

I found using regex and getmxrr() helps as you can check for MX records
for a domain name. Again not failsafe obviously, but all helps =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #6

P: n/a
Michael Fesser a écrit :
.oO(Cem Louis) (snip) Try this:
$e_mails = array();
$filename = file('mails.txt');
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) {
$line = trim($line);
if (preg_match('#^[\w-.]+@([\w-]+\.)+\w{2,4}$#', $line)) {
$e_mails[] = $line;
}
}


Bruno
Jul 17 '05 #7

P: n/a

"bruno modulix" <on***@xiludom.gro> wrote in message
news:41***********************@news.free.fr...
Michael Fesser a écrit :
.oO(Cem Louis)

(snip)
Try this:
$e_mails = array();
$filename = file('mails.txt');
> 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) {


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

- Virgil
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.