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

Robert P. J. Day rpjday at crashcourse.ca
Tue Mar 30 15:42:35 EDT 2010

On Tue, 30 Mar 2010, Chris Bruner wrote:

> 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.

  now that i think about it, even if preg_split() *looks* simpler, i
think i'll stick with a slightly tidied up version of my preg_match()
solution since, *philosophically*, it seems to match more closely what
i'm trying to do.  from my perspective, preg_split() would be the
right approach if i was in fact trying to split a string into
equivalent smaller parts.  but, really, i am trying to extract very
specific sub-strings that follow a given pattern, so preg_match()
would seem to match that problem model more closely, from a *semantic*
point of view.  and i think that's actually useful when writing code
-- to use the construct that appears to match the problem at hand.


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 mailing list