获取带有后缀的域名(仅限域名)
我需要使用PHP或JavaScript中的正则表达式来解析域。
任何后缀(com,org,net,us,ar,....)和任何协议(http,https,ftp,...)
示例:
array(
, 'http://domainname.com'
, 'http://domainname.com/whatever/you/get/the/idea'
, 'http://www.domainname.com/whatever/you/get/the/idea'
, 'http://www.domainname.com'
, 'https://s1.s2.domainname.com/321.com/www.654.vom/'
, 'https://s1.domainname.com'
, 'https://s1.domainname.com/domain1.com/'
, 'https://domain1.com.domainname.com'
, 's1.s2.domainname.com/321.com/www.654.vom/'
, 's1.domainname.com'
, 's1.domainname.com/domain1.com/'
, 'domain1.com.domainname.com'
);
结果: 对于所有结果 domainname.com
答案 0 :(得分:1)
JS,使用现代URL API:
u = 'https://s1.s2.domainname.com/321.com/www.654.vom/'
host = new URL(u).host.split('.').slice(-2).join('.')
旧式正则表达式
host = u.match(/^\w+:\/\/.*?(\w+\.\w+)(\/|$)/)[1]
答案 1 :(得分:0)
没有正则表达式的方式:
$urls = array('http://domainname.com',
'http://domainname.com/whatever/you/get/the/idea',
'http://www.domainname.com/whatever/you/get/the/idea',
'http://www.domainname.com',
'https://s1.s2.domainname.com/321.com/www.654.vom/',
'https://s1.domainname.com',
'https://s1.domainname.com/domain1.com/',
'https://domain1.com.domainname.com');
$domains = array_map(function ($url) {
$chunks = explode('.', parse_url($url,PHP_URL_HOST));
$first_level_dn = array_pop($chunks);
return array_pop($chunks) . '.' . $first_level_dn;
}, $urls);
print_r($domains);