fork download
  1. <?php
  2.  
  3. $msg1 = <<< EOD
  4. Steve,
  5.  
  6. old message
  7.  
  8. Jonathan
  9. EOD;
  10.  
  11. $msg2 = <<< EOD
  12. Jonathan,
  13.  
  14. new message
  15.  
  16. On Jul 19, 2013, at 3:28 PM, jonathan@somedomain.com wrote:
  17. {$msg1}
  18.  
  19. Thanks, Steve
  20. EOD;
  21.  
  22.  
  23. $threads = array(
  24. $msg2 => array(
  25. $msg2,
  26. $msg1,
  27. ),
  28. );
  29.  
  30.  
  31. $quote = 'On [a-z]{3} \\d{1,2}, \\d{4}, at \\d{1,2}:\\d{2} (?:AM|PM), [^@\\s]++@[^\\s]++ wrote:';
  32.  
  33. $new_threads = array();
  34.  
  35. foreach ($threads as $key => $thread) {
  36.  
  37. $tmp = array();
  38. $prev = null;
  39.  
  40. foreach (array_reverse($thread) as $email) {
  41. if ($prev === null) {
  42. $tmp[] = $prev = $email;
  43. } else {
  44. $pattern = sprintf('/(?:%s|%s)[\\s]*+/i', $quote, preg_quote($prev));
  45. $tmp[] = $prev = preg_replace($pattern, '', $email);
  46. }
  47. }
  48.  
  49. $new_threads[$key] = array_reverse($tmp);
  50.  
  51. }
  52.  
  53. foreach ($new_threads as $key => $value) {
  54. echo "[all]\n{$key}\n\n";
  55. echo "[msg2]\n{$value[0]}\n\n";
  56. echo "[msg1]\n{$value[1]}\n\n";
  57. }
  58.  
  59.  
  60.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
[all]
Jonathan,

new message

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

old message

Jonathan

Thanks, Steve

[msg2]
Jonathan,

new message

Thanks, Steve

[msg1]
Steve,

old message

Jonathan