How to get a domain name?

Hello!

Please help with PHP to get the name of the primary domain (only 1st and 2нь level) from the url.
Example:
my.mail.ru
http://www.my.mail.ru
https://mail.ru
http://test.ru
https://www.1.test.ru

To:
mail.ru
test.ru
June 26th 19 at 14:11
3 answers
June 26th 19 at 14:13
Solution
function ExtractDomain($Host, $Level = 2, $IgnoreWWW = false) {
 $Parts = explode(".", $Host);
 if($IgnoreWWW and $Parts[0] == 'www') unset($Parts[0]);
 $Parts = array_slice($Parts, -$Level);
 return implode(".", $Parts);
}
June 26th 19 at 14:15
preg_match_all('/([0-9a-z-]+\.ru)/si', $get_query, $arr);
June 26th 19 at 14:17
Mail.ru

Find more questions by tags Regular expressionsWeb DevelopmentPHP