[kwlug-disc] PHP, email addresses and regular expressions
Chris Bruner
cbruner at quadro.net
Tue Mar 30 15:25:51 EDT 2010
Robert P. J. Day wrote:
> 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:
>
> =====
>
You might want to look at explode.
I think you can do something like
array = explode("'<>@",instring);
and you will get an array of the instring divided into parts separated
by the elements of "<>@", after that, just rearrange and return what you
want.
> <?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
> ========================================================================
>
> _______________________________________________
> kwlug-disc_kwlug.org mailing list
> kwlug-disc_kwlug.org at kwlug.org
> http://astoria.ccjclearline.com/mailman/listinfo/kwlug-disc_kwlug.org
>
>
More information about the kwlug-disc
mailing list