PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 20, 2017 4:35 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri May 26, 2017 6:56 pm 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 177
Good Day Folks!

1. Is the following regex ok to extract top level domains and 2nd level domains ?
[^.]*\.[^.]{2,3}(?:\.[^.]{2,3})?$

2. How to write php code to use that regex ?
Any sample code welcome.


Top
 Profile  
 
PostPosted: Sat May 27, 2017 9:25 am 
Offline
Forum Contributor

Joined: Wed Jan 18, 2017 4:43 pm
Posts: 177
Guys,

I'm a complete beginner in regex and so any suitable tutorial suggestions for complete beginners are welcome too!

Anyway, as you know, different webpages would have different internal & external links all over their pages. No matter, what the link looks like, the domain should be extracted. Imagine, I'm running a web crawler, it would encounter unlimited links where some would have just domain and some subdomain and so on.
Eg.

http://domain.com
http://subdomain.domain.com


www.domain.com
http://www.domain.com


http://www.domain.com
http://subdomian.domain.com


domain.com/dir
subdomian.domain.com/dir

domain.com/dir/sub-dir
subdomian.domain.com/dir/sub-dir


Note: No matter how many subdomains or levels of domains (3rd level, 4th level, etc.) or dirs or sub-dirs (regardless of levels) the links contain, the 2nd level domain should be extracted along with it's tld.
From our examples above, the script should extract "domain.com" from all the above mentioned links.
I need an example of the php code too alongside the regex.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group