dongzao9044 2014-03-24 08:13
浏览 60
已采纳

php preg_match无法使用imap_fetchbody

I have the following code which works on 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]);
    }

Whereas,

This is the code I am using in Yii framework where I try to read Emails from my account, parse it and then insert it into 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]);
    }
  }
}

But preg_match is not working when I fetch the message from imap_fetchbody

Initially I thought imap_fetchbody returns encoded text but when I run gettype($message1) the type of $message1 is string.

Please let me know what am I doing wrong. Basically I want to split the fetched message in the specified pattern.

Expected output after preg_match processing

The CEO is Satya Nadella
  • 写回答

1条回答 默认 最新

  • duanping3587 2014-03-24 08:51
    关注

    I guess you need the multiline modifier:

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

    Also you don't all these capture group:

    if (preg_match("/^(.*)On (?:Mon|Tue|Wed|Thu|Fri|Sat|Sun).*$/s", $message1, $matches)) {
        echo "the messages are: <br>";
        print_r($matches[1]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP