PHP Snippets: extract all e-mails from all files in folder

<pre>
<?php

//DEFINE DIR NAME HERE
$dirname = "./folder/";

// READ DIR and put in all in to array
$allfiles = array();

if ($handle = opendir($dirname)) {
    while (false !== ($filename = readdir($handle))) {
        if ($filename != "." && $filename != "..") {
			//echo "$filename\n";
			array_push($allfiles, $filename);
        }
    }	
    closedir($handle);
}


$pattern="/(?:[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-Za-z0-9-]*[A-Za-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";


// READ FILE

// DEFINE range limit: will be taken only matching with "@" for certain number of first lines

$RFrom = 0;
$REnd = 1024;

foreach($allfiles as $val){

	$lines = file($dirname.$val); //file in to an array
	$n=0; 
	foreach($lines as $single){
			
	if($n >= $RFrom){ 
		// analyze line only if in range 
		if (strpos($single, '@') !== FALSE){
					
			preg_match_all($pattern, $single, $matches);
			$email = $matches[0][0];
			//print_r($matches);
					
			// filter out system emails such as: 2b7dbfc7-2964-4985-8e51-c17c068f2745@domain.com
			// based on count of "-"
			if( count(explode("-", $email))	< 4 ) 
			// filter out unreasonably LONG emails such as: AMSPR07MB295AC42785392AE7F31582E@...domain.com
			// based on string lenght
			if( strlen($email) < 60 )

			// filter out known e-mails such as @blazingt..  postmaster@  etc..
			// based on strpos

			if (strpos($email, 'youremail') === FALSE)
			if (strpos($email, 'postmaster@') === FALSE)
			if (strpos($email, 'MAILER-DAEMON') === FALSE)

			// if all conditions above passed succesfully:
			echo "$email \n";

			}
		}

		if($n >= $REnd) break; // breake file reading (and analisys) if out of range
		$n++;
	}
}
?>