1

我有以下结构:

$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:

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

乔纳森,

{{这里有新消息}}

史蒂夫,

{{旧消息}}

乔纳森

谢谢,史蒂夫

破折号 破折号

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

4

2 回答 2

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";
}
于 2013-07-20T22:11:14.400 回答
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);
于 2013-07-20T21:13:09.953 回答