Jump to content

[SOLVED] Regex for URL help :(


lordphate

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/[email protected]&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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.