2011-03-26 47 views
0

尝试查找REGEX以从PHP中的字符串中获取列表 - 取消订阅电子邮件标题。正则表达式获取列表 - 取消订阅标题

字符串是$ row ['emailHeader'];

目前代码:

preg_match_all('[Ll]ist-[Uu]nsubscribe:([^\r\n)', $row['emailHeader'], $matches); 

这是给我零个匹配。我正在寻找不区分大小写的“list-unsubscribe:”,直到下一个电子邮件标题开始。 list-unsubscribe通常后面跟着http://..或者http://...无论哪种方式,我可以在他们从头文件中提取这些内容时对它们进行排序,但是我遇到了正则表达式的问题。

样品头

Delivered-To: [email protected] 

Received: by 10.204.154.213 with SMTP id p21cs9880bkw; Fri, 18 Mar 2011 

02:30:10 -0700 (PDT) 

Received: by 10.216.6.27 with SMTP id 27mr896493wem.69.1300440610105; Fri, 18 

Mar 2011 02:30:10 -0700 (PDT) 

Return-Path: <[email protected]> 

Received: from omp.email.blackberry.com (omp.email.blackberry.com 

[12.130.139.56]) by mx.google.com with ESMTP id 

m9si6838771wer.130.2011.03.18.02.30.08; Fri, 18 Mar 2011 02:30:09 -0700 (PDT) 

Received-SPF: pass (google.com: domain of [email protected] 

designates 12.130.139.56 as permitted sender) client-ip=12.130.139.56; 

Authentication-Results: mx.google.com; spf=pass (google.com: domain of 

[email protected] designates 12.130.139.56 as permitted sender) 

[email protected]; dkim=pass (test mode) 

[email protected] 

DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; s=rim2; 

d=e.blackberry.com; 

h=MIME-Version:Content-Type:Date:From:Reply-To:Subject:List-Unsubscribe:To:Message-ID; 

[email protected]; bh=L4GAsbVx5z3blXLEGQxCgfBnsNc=; 

b=MHU4avprkIGvy+kVPFX2rGO60W1751sP9W19RpfSRKsPb4z+6477OvH43sHRkob3tmUxRWouXpLT 

/pbmIgRYG/ERQskQamVppPHd7lYbiLT2/VMxanhHRaiUNEE7PGRwHi5ora77hYnPdFNSprn30LE5 

WXrGgd71oqUXkf75Ytw= 

DomainKey-Signature: a=rsa-sha1; c=nofws; q=dns; s=rim2; d=e.blackberry.com; 

b=Ob29t9oVYqQYjo7MC62tC3+TDWTHxfmrcWUzMeUHyIRpPNA5LGf+IPeNN2quUIxqcCV8hnbjPbmk 

hz0Hgbw3aILzg6+5eQDtqbIBl6QIzaGSNk1rHc201/aqsPmiTvqJEny0rs7dyij+mrcNvzLyLjY8 

d/1tg+k2jf77YOHqcmI=; 

Received: by omp.email.blackberry.com (PowerMTA(TM) v3.5r15) id hgcj200morc5 

for <[email protected]>; Thu, 17 Mar 2011 13:34:05 -0700 (envelope-from 

<[email protected]>) 

MIME-Version: 1.0 

Content-Type: multipart/mixed; boundary="----msg_border" 

Date: Thu, 17 Mar 2011 13:34:05 -0700 

From: "BlackBerry" <[email protected]> 

Reply-To: "BlackBerry" <[email protected]> 

Subject: Developer News Bytes - New development tool releases! 

List-Unsubscribe: <mailto:[email protected]>, 

<http://e.blackberry.com/servlet/optout?kptKtDUAUUDBEkJHOLSSHFntHpsDJhtEf> 

X-cid: rimdm.1711.8 

X-sgxh1: jJHNLFFHxnuHptQJhu 

To: [email protected] 

Message-ID: <[email protected]> 

理想情况下,我只想要第二列表退订,但我可以处理瓦特/让双方和搜索。

回答

0
preg_match_all("#List-Unsubscribe:(.*)#i", $row['emailHeader'], $matches); 
+0

谢谢!唯一缺失的是它只有在有两个值时返回值1,即ListUnsubscribe:,它退回直到取消订阅类型后的逗号 – 2011-03-26 03:53:43

+0

它在某些情况下实际显示它们,但不是全部。 – 2011-03-26 03:58:17

+0

请发布$ row ['emailHeader']的内容,我会重写它,然后print_r($匹配)来检查 – 2011-03-26 03:58:29

0

必须使用: ^ - 线 开始$ - 行尾 - 多以匹配所有行

// join headers. It's possible headers to be split to 2 rows 
$header = preg_replace("@\r\n\[email protected]", " ", $row['emailHeader']); 

// i is for case insensitive, m is for multiline 
preg_match_all('/^list\\-unsubscribe:(.*)$/mi', $headers, $matches);