正则表达式获取列表 - 取消订阅标题

时间:2011-03-26 03:25:57

标签: php regex preg-match

尝试查找REGEX以从PHP中的字符串中获取电子邮件标头中的列表取消订阅。

string是$ row ['emailHeader'];

当前代码:

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

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

示例标题

Delivered-To: rcavezza@gmail.com

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: <BlackBerry@e.blackberry.com>

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 BlackBerry@e.blackberry.com

 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

 BlackBerry@e.blackberry.com designates 12.130.139.56 as permitted sender)

 smtp.mail=BlackBerry@e.blackberry.com; dkim=pass (test mode)

 header.i=BlackBerry@e.blackberry.com

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;

 i=BlackBerry@e.blackberry.com; 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 <rcavezza@gmail.com>; Thu, 17 Mar 2011 13:34:05 -0700 (envelope-from

 <BlackBerry@e.blackberry.com>)

MIME-Version: 1.0

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

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

From: "BlackBerry" <BlackBerry@e.blackberry.com>

Reply-To: "BlackBerry" <reply@e.blackberry.com>

Subject: Developer News Bytes - New development tool releases!

List-Unsubscribe: <mailto:unsubscribe-rimdm.1711@im4.responsys.net>,

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

X-cid: rimdm.1711.8

X-sgxh1: jJHNLFFHxnuHptQJhu

To: rcavezza@gmail.com

Message-ID: <0.0.19.2D3.1CBE4E2A90A4BD0.0@omp.email.blackberry.com>

理想情况下,我想要第二个列表 - 仅取消订阅,但我可以处理两者并进行搜索。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你必须使用:   ^ - 开始行   $ - 结束   m - 多行匹配所有行

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

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