PHP RegEx Validation

Originally from I'm Mike

/**
 * Match alphanumeric and underscore username
 * 3-16 characters long
 */
function validate_username( $username ) {
  return preg_match('/^[a-zA-Z0-9_]{3,16}$/', $_GET['username']);
  //returns true if match, and false if not match.
}

Reference: http://www.phpro.org/examples/Strip-All-Non-Alphanumeric-Characters-Except-Space-With-PHP.html

/**
* Remove all non alpha numeric characters except a space
* @param    string    $string The string to cleanse
* @return    string
*/
function alphanumericAndSpace( $string ) {
	return preg_replace('/[^a-zA-Z0-9\s]/', '', $string);
}

Originally from http://www.linuxjournal.com/article/9585

/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
 
	// PHP in windows does not have the function checkdnsrr until version 5.30;
/* FOR WINDOWS USE FOLLOWING FUNCTION ALSO;
function customCheckDnsrr($host,$recType='') {
 if(!empty($host)) {
  if($recType=='') $recType="MX";
  exec("nslookup -type=$recType $host",$output);
  foreach($output as $line) {
   if(preg_match("/^$host/", $line)) {
    return true;
   }
  }
  return false;
 }
 return false;
}
*/
 
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}
/**
 * Email Validation
 * http://www.iamcal.com/publish/articles/php/parsing_email/
 * Explanation: http://www.iamcal.com/publish/articles/php/parsing_email/
 */
function is_valid_email_address($email){
  $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
                  '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  $quoted_pair = '\\x5c[\\x00-\\x7f]';
  $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
  $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
  $domain_ref = $atom;
  $sub_domain = "($domain_ref|$domain_literal)";
  $word = "($atom|$quoted_string)";
  $domain = "$sub_domain(\\x2e$sub_domain)*";
  $local_part = "$word(\\x2e$word)*";
  $addr_spec = "$local_part\\x40$domain";
 
  /* //Can also use below only instead of all the above code, but harder to read;
  $addr_spec = '([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
			'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d'.
			'\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)'.
			'(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e'.
			'\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|'.
			'\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00'.
			'-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28'.
			'\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d'.
			'\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff'.
			']|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20'.
			'\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40'.
			'\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-'.
			'\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))*';
   */
 
 
  return preg_match("!^$addr_spec$!", $email) ? 1 : 0;
}

Modified from http://codesnippets.joyent.com/posts/show/179

/** 
*/
 
function extract_emails_from($string){
   // Explanation: http://www.iamcal.com/publish/articles/php/parsing_email/
   $addr_spec = '([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
			'\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d'.
			'\\x22\\x5c\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x22)'.
			'(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e'.
			'\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|'.
			'\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00'.
			'-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28'.
			'\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d'.
			'\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff'.
			']|\\x5c[\\x00-\\x7f])*\\x5d)(\\x2e([^\\x00-\\x20'.
			'\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40'.
			'\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^\\x0d\\x5b-'.
			'\\x5d\\x80-\\xff]|\\x5c[\\x00-\\x7f])*\\x5d))*';
  preg_match_all("!^$addr_spec$!", $string, $matches);
  return $matches[0];
}
 
$text = "blah blah blah email@address.com blah blah blah email2@address.com";
 
$emails = extract_emails_from($text);
 
print(implode("\n", $emails));