473,410 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

An array question???

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
7 1909
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
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
.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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
9
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
3
by: Pol Bawin | last post by:
Hi All, One : I have a property that get/set a array of an abstract class A By default my array is null In the propertygrid, It is not works correctly when my array is null. (when my array...
11
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^...
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
8
by: T. Wintershoven | last post by:
Hello all, I have a form with some checkboxes. The names of these checkboxes come from an array. When i click the submit button the resultcode doesn't recognize the names when i want to check...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.