afrojojo Posted February 15, 2010 Share Posted February 15, 2010 I am trying to search an email header for the email address after it was piped to the script. This is what the header and code look like: Header From me@mysite.com Sun Feb 14 17:45:41 2010 Code if (preg_match("/^From (.*)/", $lines[$i], $matches)) { $from = $matches[1]; } Outputs "me@mysite.com Sun Feb 14 17:45:41 2010" I want it to output just "me@mysite.com" Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/ Share on other sites More sharing options...
teamatomic Posted February 15, 2010 Share Posted February 15, 2010 /^From (\S+)/ this will put the address at [1] HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012377 Share on other sites More sharing options...
cags Posted February 15, 2010 Share Posted February 15, 2010 Bare in mind that will only work if a name hasn't been included in the From header. If that's not a problem then your good to go, if it is let us know. I'm not going to sit here and work something out if it's unneeded. Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012496 Share on other sites More sharing options...
afrojojo Posted February 15, 2010 Author Share Posted February 15, 2010 What if the header looks like this and is in an $email variable: Header From me@mysite.com Mon Feb 15 11:34:42 2010 Received: from xx-xx-xx-xx.dhcp.eucl-nbb.wi.charter.com ([xx-xx-xx-xx] helo=[xx-xx-xx-xx]) by xx.xx.com with esmtpa (Exim 4.69) (envelope-from ) id 1Nh5mL-00035o-Ti for rsv Code if (preg_match("/^From (\S+)/", $email[$i], $matches)) { $from = $matches[1]; } It doesn't output anything. I want it to output "me@mysite.com". Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012739 Share on other sites More sharing options...
cags Posted February 15, 2010 Share Posted February 15, 2010 Do you have an $email array and is $i a valid key in it? If so, just echo $from. Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012742 Share on other sites More sharing options...
afrojojo Posted February 15, 2010 Author Share Posted February 15, 2010 Do you have an $email array and is $i a valid key in it? If so, just echo $from. No, email contains that header string. I'm not sure where $i comes from. echoing $from doesn't show anything. I don't fully understand preg_match, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012748 Share on other sites More sharing options...
cags Posted February 15, 2010 Share Posted February 15, 2010 If your not sure where it's from, why is it there? Of course it doesn't output anything, hence the fact I had the big fat if at the start of my post. If $email contains the header then it should be... if (preg_match("/^From (\S+)/", $email, $matches)) { $from = $matches[1]; } Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012752 Share on other sites More sharing options...
afrojojo Posted February 15, 2010 Author Share Posted February 15, 2010 Yeah, im an idiot. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/192089-preg_match-help/#findComment-1012763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.