从最新的线程php删除旧电子邮件

时间:2013-07-20 20:51:32

标签: php email imap

我有以下结构:

$email = array(
      4 => array(
          4,  //contains email messages from uids 4 + 3 + 2
          3,  //contains email messages from uids 3 + 2
          2   //contains email message from uid 2
));

我想要完成的是:

$email = array(
      4 => array(
          4,  //contains email messages from uid 4
          3,  //contains email messages from uid 3
          2   //contains email message from uid 2
));

我的想法是我需要从堆栈的末尾获取消息,并递归地向上遍历树并对内容执行str_replace,只留下该特定电子邮件中的内容。我无法弄清楚如何做到这一点。

此外,

我注意到有些情况例如在查看消息uid 4时会出现类似的情况:

On Jul 19, 2013, at 3:28 PM, jonathan@somedomain.com wrote:

有时它会显示新消息,而不是旧消息,而不是新消息的签名:

乔纳森,

{{new message here}}

  

史蒂夫,

     

{{old message}}

     

乔纳森

谢谢, 史蒂夫

破折号破折号

我不知道如何实现这一点。我希望我能正确地提出我的问题并提供足够的信息来提供帮助。

2 个答案:

答案 0 :(得分:0)

你的意思是这样吗?

<?php

$threads = array(
    'baz bar foo' => array(
        'baz bar foo',
        'bar foo',
        'foo',
    ),
);

$new_threads = array();

foreach ($threads as $key => $thread) {

    $tmp = array();
    $prev = null;

    foreach (array_reverse($thread) as $email) {
        switch (true) {
            case $prev === null;
            case ($pos = strrpos($email, $prev)) === false:
                $tmp[] = $email;
                $prev = $email;
                break;
            default:
                $tmp[] = rtrim(substr($email, 0, $pos));
                $prev = $email;
        }
    }

    $new_threads[$key] = array_reverse($tmp);

}

var_dump($new_threads);

答案 1 :(得分:0)

它有效! http://ideone.com/qXysl3

<?php

$msg1 = <<< EOD
Steve,

old message

Jonathan
EOD;

$msg2 = <<< EOD
Jonathan,

new message

On Jul 19, 2013, at 3:28 PM, jonathan@somedomain.com wrote:
{$msg1}

Thanks, Steve
EOD;


$threads = array(
    $msg2 => array(
        $msg2,
        $msg1,
    ),
);


$quote = 'On [a-z]{3} \\d{1,2}, \\d{4}, at \\d{1,2}:\\d{2} (?:AM|PM), [^@\\s]++@[^\\s]++ wrote:';

$new_threads = array();

foreach ($threads as $key => $thread) {

    $tmp = array();
    $prev = null;

    foreach (array_reverse($thread) as $email) {
        if ($prev === null) {
            $tmp[] = $prev = $email;
        } else {
            $pattern = sprintf('/(?:%s|%s)[\\s]*+/i', $quote, preg_quote($prev));
            $tmp[] = $prev = preg_replace($pattern, '', $email);
        } 
    }

    $new_threads[$key] = array_reverse($tmp);

}

foreach ($new_threads as $key => $value) {
    echo "[all]\n{$key}\n\n";
    echo "[msg2]\n{$value[0]}\n\n";
    echo "[msg1]\n{$value[1]}\n\n";
}