[kwlug-disc] PHP, email addresses and regular expressions

Robert P. J. Day rpjday at crashcourse.ca
Tue Mar 30 14:50:00 EDT 2010


  i want to write a PHP function that takes a moderately sane email
address and breaks it into its components and returns those pieces as
an array.  there are only two possibilities i will accept:

  full addresses:  "Robert P. J. Day <rpjday at crashcourse.ca>"
  short addresses: rpjday at crashcourse.ca

what i want to get back is, in both cases above, the first two
elements of the array being the username ("rpjday") and domain
("crashcourse.ca"), and *then*, in the full case, the third element of
the array will be the full name ("Robert P. J. Day").  on totally
invalid email address, NULL shall be returned.

  in a few minutes, i lashed together the following:

=====

<?php

function break_email_into_pieces($arg)
{
    $full_ptn = '/^(.+) *< *(.+)@(.+)>$/';
    $short_ptn = '/^([^<]+)@([^>]+)$/';

    if (preg_match($full_ptn, $arg, $m)) {
        return array($m[2], $m[3], trim(trim($m[1]), '"'));
    } else if (preg_match($short_ptn, $arg, $m)) {
        return array($m[1], $m[2]);
    } else {
        return NULL;
    }
}

$addr = 'Robert P. J. Day <rpjday at crashcourse.ca>';
var_dump(break_email_into_pieces($addr));

$addr = '"Robert P. J. Day" <rpjday at crashcourse.ca>';
var_dump(break_email_into_pieces($addr));

$addr = '<rpjday at crashcourse.ca>';
var_dump(break_email_into_pieces($addr));

$addr = 'rpjday at crashcourse.ca';
var_dump(break_email_into_pieces($addr));

?>

=====

  it all seems to work and i don't need the routine to be
spectacularly robust since i'm extracting the addresses from emails as
they go flying by so i can assume there's a good chance they're
RFC3696-compatible and i'm just grabbing strings out of the To: and
From: fields of those addresses.

  thoughts?  anything critical i'm missing?  any elegant improvements?
it looks good now but i'm always open to good advice.

rday
--

========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================



More information about the kwlug-disc_kwlug.org mailing list