Jump to content

http(s) url regex


Lectrician

Recommended Posts

Hi.  I am trying to extract the main url from a string.

 

For example.

 

I want to extract:

 

"http://google.com" from "http://google.com/search/pic.jpg"

"https://secure.co.uk" from "https://secure.co.uk/secure/secure.gif"

 

I am able to do this with this code, where $url is the url:

 

preg_match("http://(.*?)/", $url, $matches);

preg_match("https://(.*?)/", $url, $matches);

 

Is there away to consolidate this into one regex search?  Or am I being picky?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/289023-https-url-regex/
Share on other sites

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.