jakebur01 Posted June 27, 2011 Share Posted June 27, 2011 "How can I extract the fax # out of this e-mail message? I will either be using a 3rd party program that handles returned messages or build a little mail reading script that will handle this. I want to extract the fax number located between "To fax # :" and "From fax # :". Here is a copy of the e-mail I will receive when the fax failed: Status for VSI-FAX fax job: 1025 Submitted by: vsifax E-mail : myname@testsite.com Submit time : 06/24 11:15 Queue : fax1 Result : expired Status : Max tries reached To name : <none> From name : VSIFAX Administrator To company : <none> From company: <none> To fax # : 555-555-3551 From fax # : <none> To voice # : <none> From voice #: <none> Subject : <none> Num dialed : 15048323291 usrtag1 : 001210 Cover : 1 pg default File 1 : 2 pgs /u/ssc/ssi7/docdel/001210.vsi Total pgs : 3 pgs Attempt Send-Time Result CSI ------- ----------- ------------------ ---------------- 1 06/24 11:16 Timed out <none> 2 06/24 11:22 No carrier <none> 3 06/24 11:29 Timed out <none> 4 06/24 11:36 Timed out <none> 5 06/24 11:42 Timed out <none> Link to comment https://forums.phpfreaks.com/topic/240544-failed-faxes/ Share on other sites More sharing options...
cssfreakie Posted June 27, 2011 Share Posted June 27, 2011 you could use the function preg_match_all for this. http://www.php.net/manual/en/function.preg-match-all.php as an example. <?php error_reporting(E_ALL); ini_set("display_errors", 1); $string = "Status for VSI-FAX fax job: 1025 Submitted by: vsifax E-mail : myname@testsite.com Submit time : 06/24 11:15 Queue : fax1 Result : expired Status : Max tries reached To name : <none> From name : VSIFAX Administrator To company : <none> From company: <none> To fax # : 555-555-3551 From fax # : 555-123-9876 To voice # : <none> From voice #: <none> Subject : <none> Num dialed : 15048323291 usrtag1 : 001210 Cover : 1 pg default File 1 : 2 pgs /u/ssc/ssi7/docdel/001210.vsi Total pgs : 3 pgs Attempt Send-Time Result CSI ------- ----------- ------------------ ---------------- 1 06/24 11:16 Timed out <none> 2 06/24 11:22 No carrier <none> 3 06/24 11:29 Timed out <none> 4 06/24 11:36 Timed out <none> 5 06/24 11:42 Timed out <none>"; $subject = $string; $pattern = '~fax\s+#\s+:\s+([0-9-]+)~'; preg_match_all($pattern, $subject, $matches); var_dump($matches); ?> note sure if the above is the most optimized regex for this case since i had to guess the format. Hope it helps a bit Link to comment https://forums.phpfreaks.com/topic/240544-failed-faxes/#findComment-1235586 Share on other sites More sharing options...
.josh Posted June 27, 2011 Share Posted June 27, 2011 Here's my take... $pattern = '~fax\s+#\s+:\s+\K(??!from).)+~i'; preg_match($pattern, $subject, $matches); $fax = trim($matches[0]); So first off, since he's just grabbing one number, preg_match should be used instead of preg_match_all. 2nd, to accommodate random fax number formats, I use a negative lookahead and just match everything up to the next "from" and then trim the extra whitespace. Link to comment https://forums.phpfreaks.com/topic/240544-failed-faxes/#findComment-1235617 Share on other sites More sharing options...
cssfreakie Posted June 27, 2011 Share Posted June 27, 2011 Nice one Crayon I had the impression he was looking for 2 numbers, but it was one in between. negative lookaheads, i should read more about that cheers Link to comment https://forums.phpfreaks.com/topic/240544-failed-faxes/#findComment-1235622 Share on other sites More sharing options...
.josh Posted June 27, 2011 Share Posted June 27, 2011 Quote Here's my take... $pattern = '~fax\s+#\s+:\s+\K(??!from).)+~i'; preg_match($pattern, $subject, $matches); $fax = trim($matches[0]); Actually, I can just just put the trailing whitespace in the negative lookahead and skip the trim... $pattern = '~fax\s+#\s+:\s+\K(??!\s*from).)+~i'; preg_match($pattern, $subject, $matches); $fax = $matches[0]; Link to comment https://forums.phpfreaks.com/topic/240544-failed-faxes/#findComment-1235630 Share on other sites More sharing options...
jakebur01 Posted June 28, 2011 Author Share Posted June 28, 2011 Thank you crayonviolent. This is exactly what I had in mind. Thank you cssfreakie. Jake Quote Here's my take... $pattern = '~fax\s+#\s+:\s+\K(??!from).)+~i'; preg_match($pattern, $subject, $matches); $fax = trim($matches[0]); So first off, since he's just grabbing one number, preg_match should be used instead of preg_match_all. 2nd, to accommodate random fax number formats, I use a negative lookahead and just match everything up to the next "from" and then trim the extra whitespace. Link to comment https://forums.phpfreaks.com/topic/240544-failed-faxes/#findComment-1235852 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.