2010-08-04 76 views
0

我想解析郵件收發器到字符串這可能包括用逗號分隔的以下實施例(,):幫助想與正則表達式

First name <[email protected]> 
"first name" <[email protected]> 
<[email protected]> 
[email protected] 

我想使一個陣列中的條目每個元素都有兩個子條目:[name]和[email]。

我一直在努力與正則表達式(看起來像)的年齡。有人能幫助我嗎?

+0

要進行正則表達式更容易,你可以在一個循環中分析它一行行,那麼每個正則表達式可能看起來像/(["\w\s]+)?\s?// – 2010-08-04 15:10:20

回答

1

如果你啓用了IMAP的擴展,它可以是簡單的:

var_dump(imap_rfc822_parse_adrlist('First name <[email protected]>, 
    "first name" <[email protected]>, 
    <[email protected]>, 
    [email protected]','_invalid_')); 

輸出:

array(4) { 
    [0]=> 
    object(stdClass)#1 (3) { 
    ["mailbox"]=> 
    string(5) "email" 
    ["host"]=> 
    string(11) "example.com" 
    ["personal"]=> 
    string(10) "First name" 
    } 
    [1]=> 
    object(stdClass)#2 (3) { 
    ["mailbox"]=> 
    string(5) "email" 
    ["host"]=> 
    string(11) "example.com" 
    ["personal"]=> 
    string(10) "first name" 
    } 
    [2]=> 
    object(stdClass)#3 (2) { 
    ["mailbox"]=> 
    string(5) "email" 
    ["host"]=> 
    string(11) "example.com" 
    } 
    [3]=> 
    object(stdClass)#4 (2) { 
    ["mailbox"]=> 
    string(5) "email" 
    ["host"]=> 
    string(11) "example.com" 
    } 
} 
+0

噢,這樣會輕鬆很多! – richardverbruggen 2010-08-04 15:17:29

+0

解析符合rfc822規範的電子郵件地址的正則表達式有點複雜。http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html +1 – Buggabill 2010-08-04 15:27:48

+0

@Buggabill正則表達式實際上並不需要驗證電子郵件地址,只需在主字符串中找到它,這並不是特別困難 – 2010-08-04 15:37:56