Page 1 of 1

Nice Regular expression problem

Posted: Fri May 02, 2003 2:10 pm
by pascalganaye
I need to extract emails and name from the sender part of an email address.

The sender has the usual form : Pascal GANAYE <p.ganaye@bigfot.com>
But it can also be : "Pascal GANAYE <p.ganaye@bigfot.com>"
Or : " Pascal GANAYE " <p.ganaye@bigfot.com >
Or : Pascal GANAYE [p.ganaye@bigfot.com]
Or : < p.ganaye@bigfot.com>
There is probably more syntax...

From this string I nead to extract both the Name (when available) and the email address.

I started to write :roll: :

Code: Select all

if (preg_match("/&#1111;^\s^"^']+@&#1111;^\s^"^']+/",$email,$res)) &#123;
    $email = $res&#1111;0];
    $name = preg_replace("/&#1111;"'\s]*<.*>\s*&#1111;"'\s]*/","",$email);
  &#125;
  ...
But I realized the problem is perhaps already discussed somewhere.
I found a lot of regular expression for validation but nothing very appropriate for an analysis and name/email extraction from it.

Anybody can help me to find an elegant solution on this problem ?