Regex To Grab Domain

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
UniqueIdeaMan
Forum Contributor
Posts: 197
Joined: Wed Jan 18, 2017 3:43 pm

Regex To Grab Domain

Post by UniqueIdeaMan »

Folks,

I need a regex to extract the domain name (2nd level) from any url. No matter what the url looks like. Whether it has more than one dot (2nd level, 3rd level, 4th level, 5th level, etc. level domain) and whether or not it has the http://, https://, www., etc.).
In short, it must work on any url in the world. No matter what format the url is in aslong as the url is a valid one. No matter how good, ugly, short or long the url is.
I am spoiled for choice as too many samples are provided here and I don't know which one will best suit my purpose:
https://stackoverflow.com/questions/569 ... e-from-url

Which one is your best pick for my purpose and why that one over the others ?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Regex To Grab Domain

Post by requinix »

You've already been told the answer.
Locked