Re: running conditions while looping through arrays....

From: Date: Mon, 19 Jun 2006 17:09:26 +0000
Subject: Re: running conditions while looping through arrays....
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-238304@lists.php.net to get a copy of this message
IG wrote:
I want to run filters on an pop3 inbox. I have the following arrays which I'll get from the database- $subject $email $body $subject_like $email_like $body_like I then go through each email in turn using this loop- $popy = "{".$pop3."/pop3:110}INBOX"; $mailbox = imap_open($popy, $un, $pw);
    $num_messages = imap_num_msg($mailbox);
if ($num_messages <= $no_srch) {$rrt = 0;} else {$rrt = $num_messages-$no_srch;}
for($i=$num_messages; $i>($rrt); $i--) { What I want to do is to check if the email address or subject or body of each email is exactly like any of the details stored in the arrays $subject, $email, $body and to check if the email address or subject or body contains a word or phrase from the arrays $subject_like, $email_like, $body_like. For example- $subject[0] = "SPAM"; $email[0]= ""; $body[0] = ""; $subject_like[0] = ""; $email_like[0] = ""; $body_like[0] = ""; // If the subject of the email = "spam" $subject[1] = ""; $email[1]= "spam@spammer.com"; $body[1] = ""; $subject_like[1] = ""; $email_like[1] = ""; $body_like[1] = ""; // if the email address of the email = "spam@spammer.com" $subject[2] = ""; $email[2]= ""; $body[2] = "spam body text"; $subject_like[2] = ""; $email_like[2] = ""; $body_like[2] = ""; // if the body of the email = "spam body text" $subject[3] = "SPAM"; $email[3]= "spam@spammer.com"; $body[3] = ""; $subject_like[3] = ""; $email_like[3] = ""; $body_like[3] = ""; // if the subject of the email = "SPAM" AND the email address = "spam@spammer.com" $subject[4] = "SPAM"; $email[4]= ""; $body[4] = ""; $subject_like[4] = ""; $email_like[4] = ""; $body_like[4] = "spam text"; // if the subject of the email = "SPAM" AND the body contains "spam text" If any of the above conditions are met then the email message is then stored in a database and deleted of the POP3 server. Is there a quick way of doing the above? The only way I can think of is by looping through each array for every email. This is really really slow. I'd be grateful for some help here.... Ian
Unless I'm mistaken, for ( $x = 0; $x < count($subject); $x++ ) { if ( $subject[$x] == "SPAM" || $email[$x] == "spam@spammer.com" || $body[$x] == "spam body text") { # Mail is spam! Do something. } } Or something like that. I'm not exactly sure what your criteria are. What I'm doing above is looping through the mail messages one at a time, and checking all aspects of that email in each loop iteration. If you're doing complex analysis on each message, then that is probably the fastest way. If you're simply searching for things, you could always do array searches. So, search through $email with array_search or perhaps array_intersect. Then you don't need to loop at all. Regards, Adam Zey.

« previous php.general (#238304) next »