fork download
  1. <?php
  2.  
  3. $data = "from remote.intra.domain.net (sbs-2011.intra.local [0.0.0.0]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mailhost.domain.net (Postfix) with ESMTPS id A2E067029D9 for <john.doe@domain.net>";
  4.  
  5. function extractComments($data)
  6. {
  7. $comments = [];
  8. $index = null;
  9. $parensCounter = 0;
  10.  
  11. foreach(str_split($data) as $char)
  12. {
  13. if($char == '(' && !isset($index))
  14. {
  15. $index = count($comments);
  16. $comments[$index] = '';
  17. }
  18. else if($char == '(')
  19. {
  20. $parensCounter++;
  21. $comments[$index] .= $char;
  22. }
  23. else if($char == ')' && $parensCounter != 0)
  24. {
  25. $parensCounter--;
  26. $comments[$index] .= $char;
  27. }
  28. else if($char == ')')
  29. {
  30. $parensCounter = 0;
  31. $index = null;
  32. }
  33. else if(isset($index))
  34. {
  35. $comments[$index] .= $char;
  36. }
  37. }
  38.  
  39. return $comments;
  40. }
  41.  
  42. function extractDomain($data)
  43. {
  44. $matches = [];
  45.  
  46. preg_match('#from\s(.+)\s+#U', $data, $matches);
  47.  
  48. return $matches[1];
  49. }
  50.  
  51. var_dump(array_merge([extractDomain($data)], extractComments($data)));
Success #stdin #stdout 0.01s 24144KB
stdin
Standard input is empty
stdout
array(5) {
  [0]=>
  string(23) "remote.intra.domain.net"
  [1]=>
  string(30) "sbs-2011.intra.local [0.0.0.0]"
  [2]=>
  string(49) "using TLSv1 with cipher AES128-SHA (128/128 bits)"
  [3]=>
  string(31) "No client certificate requested"
  [4]=>
  string(7) "Postfix"
}