lordphate Posted November 12, 2009 Share Posted November 12, 2009 Hi Everyone, I'm building a script to pull a URL from an email being read by a PHP script using php://stdin Currently I have the ability to check for email addresses (which works with no problems), and basic Regex stuff like : if(preg_match("/\b5.5.0\b/i",$message)) { ... and preg_match_all("/\bFrom [\w\.-]+@[\w\.-]+\.\w{2,4}\b/i",$headers,$emails); however when it comes down to retrieving a URL from the message I can't get past it if(preg_match('/https?\:\/\/[^\" ]+/i',$message,$url)) { Any ideas on what i'm doing wrong ? here is a sample email: Click the link below to fill out the request: https://words.morewords.domain.tld/word/addme?a=email@domain.tld&id=1n4Hlb4iO3Nl34g1 Thanks in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/181303-solved-regex-for-url-help/ Share on other sites More sharing options...
thebadbad Posted November 12, 2009 Share Posted November 12, 2009 It probably fails (in only grabbing the URL) because line breaks also are matched with your character class [^\" ]. There is heaps and heaps of complex URL regex patterns out there, but a really simple one could be ~\bhttps?://\S+~i It searches for a string starting with http:// or https:// (without a word character just before it) and then keeps on matching any character not a white space (space, tab, line break etc.). Quote Link to comment https://forums.phpfreaks.com/topic/181303-solved-regex-for-url-help/#findComment-956494 Share on other sites More sharing options...
lordphate Posted November 12, 2009 Author Share Posted November 12, 2009 It probably fails (in only grabbing the URL) because line breaks also are matched with your character class [^\" ]. There is heaps and heaps of complex URL regex patterns out there, but a really simple one could be ~\bhttps?://\S+~i It searches for a string starting with http:// or https:// (without a word character just before it) and then keeps on matching any character not a white space (space, tab, line break etc.). I tried this, and sadly it didn't work I'm not sure why it's not working. For some reason it's just not picking it up Quote Link to comment https://forums.phpfreaks.com/topic/181303-solved-regex-for-url-help/#findComment-956500 Share on other sites More sharing options...
thebadbad Posted November 12, 2009 Share Posted November 12, 2009 Works for me: <?php $message = 'Click the link below to fill out the request: https://words.morewords.domain.tld/word/addme?a=email@domain.tld&id=1n4Hlb4iO3Nl34g1'; if (preg_match('~\bhttps?://\S+~i', $message, $url)) { echo $url[0]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/181303-solved-regex-for-url-help/#findComment-956505 Share on other sites More sharing options...
lordphate Posted November 13, 2009 Author Share Posted November 13, 2009 Your right, the message was base64 encoded, so I used fwrite($fh,print_r(base64_decode($message),TRUE)); Your code works w00t Thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/181303-solved-regex-for-url-help/#findComment-956533 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.