Welcome Guest, Not a member yet? Register   Sign In
Email Parsing Helper
#1

[eluser]Kyle Johnson[/eluser]
Hey Everyone,

I'm not sure if I posted this before, but I love refinding fun bits of code as you're diving through and making changes to how things are organized. Parts of it were written by me, parts from this article here (http://www.linuxjournal.com/article/9585).

I find it super useful for validating user input:

Code:
/**
* Returns an array of validated email addresses.
* @param mixed $email Email addresses to be checked, can be an array of arrays, mixed separators, anything.
* @return array An array of valid email addresses based on the data sent to the function.
*/
function parse_email($email = array()) {
        $temp = array();
        $emailArr = array();
        if (is_array($email)) {
            foreach ($email as $item) {
                $temp = array_merge(parse_email($item), $temp); //recursion
            }
        } else {
            $mainDelimeter = ","; //replace all delimiters with this one
            $delimeters = array(";", " ", ","); //valid delimeters to split apart

            foreach ($delimeters as $delimiter) {
                $email = str_replace($delimiter, $mainDelimeter, $email); //replace all delimiters with main delimiter
            }
            $temp = explode($mainDelimeter, $email); // create array of emails
        }
        for ($index = 0; $index < count($temp); $index++) {
            $temp[$index] = trim($temp[$index]); //trim whitespace
            if (strlen($temp[$index]) > 0) { // only look at email if len > 0
                if (valid_email($temp[$index])) { // check that email address is valid, optionally can verify DNS name
                    $emailArr[] = $temp[$index]; // add to returned email list
                } else {
                   // $emailArr[] = "INVALID EMAIL: {$temp[$index]}";
                }
            }
        }
        $emailArr = array_unique($emailArr); // remove duplicates
        sort($emailArr); // sort alphabetically
        return $emailArr;
    }

    /**
      Validate an email address.
      Provide email address (raw input)
      Returns true if the email address has the email
      address format and the domain exists.
http://www.linuxjournal.com/article/9585
http://www.ilovejackdaniels.com/php/email-address-validation
     */
    function valid_email($email, $checkDNS = FALSE) {
        $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;
                }
            }
            if ($checkDNS == TRUE) {
                if ($isValid && !(checkdnsrr($domain, "MX") || checkdnsrr($domain, "A"))) {
                    // domain not found in DNS
                    $isValid = false;
                }
            }
        }
        return $isValid;
    }


Usage:
Code:
$myArr = array();

  $myArr[] = '[email protected],jb';
  $myArr[] = '[email protected],[email protected]';
  $myArr[] = '[email protected], [email protected]; [email protected] [email protected];[email protected]';
  $myArr[] = '[email protected]; [email protected]';
  $myArr[] = '[email protected];se.com;us.me.com;user1@usb;[email protected]';

  echo "myArr array<pre>";
  print_r($myArr);
  echo "</pre>";

  $myArr = parse_email($myArr);

  echo "parsed myArr array<pre>";
  print_r($myArr);
  echo "</pre>";


  $myArr = parse_email('[email protected],[email protected]; [email protected], [email protected],dummy');
  echo "string only<pre>";
  print_r($myArr);
  echo "</pre>";

  $myArr = parse_email('[email protected]');
  echo "single email only<pre>";
  print_r($myArr);
  echo "</pre>";


Output:
Code:
myArr array
Array
(
    [0] => [email protected],jb
    [1] => [email protected],[email protected]
    [2] => [email protected], [email protected]; [email protected] [email protected];[email protected]
    [3] => [email protected]; [email protected]
    [4] => [email protected];se.com;us.me.com;user1@usb;[email protected]
)
parsed myArr array
Array
(
    [0] => [email protected]
    [1] => [email protected]
    [2] => [email protected]
    [3] => [email protected]
    [4] => [email protected]
    [5] => [email protected]
    [6] => [email protected]
    [7] => user1@usb
    [8] => [email protected]
    [9] => [email protected]
    [10] => [email protected]
    [11] => [email protected]
    [12] => [email protected]
)
string only
Array
(
    [0] => [email protected]
    [1] => [email protected]
    [2] => [email protected]
    [3] => [email protected]
)
single email only
Array
(
    [0] => [email protected]
)




Theme © iAndrew 2016 - Forum software by © MyBB