php preg_match无法使用imap_fetchbody

时间:2014-03-24 08:13:16

标签: php preg-match

我有以下代码适用于Online php complier

 $message1 = "The CEO is Satya Nadella On Mon, Mar 24, 2014 at 11:09 AM, wrote: > Hello ajey_teacher, > > User *ajey * has posted the following question on your course 'D' > > Question title: Who is the CEO of microsoft? > > Question description: -- Thanks, Ajey Charantimath, 973 986 4763";

 if (preg_match("/^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)$/", $message1, $matches)) {
      echo "the messages are: <br>";
      print_r($matches[1]);
    }

然而,

这是我在Yii框架中使用的代码,我尝试从我的帐户中读取电子邮件,解析它然后将其插入到mysql中。

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'UNSEEN');

if($emails) {

  foreach($emails as $email_number) {

    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message1 = imap_fetchbody($inbox,$email_number,1);
    $header = imap_headerinfo($inbox, $email_number);

    echo "type is---->".gettype($message1);

    echo $message1;

    if (preg_match("/^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)$/", $message1, $matches)) {
      echo "the messages are: <br>";
      print_r($matches[1]);
    }
  }
}

但是当我从imap_fetchbody

获取消息时,preg_match无法正常工作

最初我认为imap_fetchbody会返回编码文本,但是当我运行gettype($message1)时,$ message1的类型是字符串。

请让我知道我做错了什么。 基本上我想以指定的模式拆分获取的消息。

preg_match处理后的预期输出

The CEO is Satya Nadella

1 个答案:

答案 0 :(得分:1)

我想你需要多线修改器:

if (preg_match("/^(.*)(On (Mon|Tue|Wed|Thu|Fri|Sat|Sun).*)$/s", $message1, $matches)) {
    //                                              here ___^
    echo "the messages are: <br>";
    print_r($matches[1]);
}

此外,您不是所有这些捕获组:

if (preg_match("/^(.*)On (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun).*$/s", $message1, $matches)) {
    echo "the messages are: <br>";
    print_r($matches[1]);
}