Jump to content

Recommended Posts

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!!

 

Link to comment
https://forums.phpfreaks.com/topic/181303-solved-regex-for-url-help/
Share on other sites

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.).

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

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];
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.