简体   繁体   中英

domain name from URL in Php

i want to extract only domain name from given URL for any kind of TLD's . TLD can have any number in character or my be two in size

  <?php $domain_name = 'http://www.subdomain.domain.co.in' ;
        $ParsedURL = parse_url($domain_name);
        $domain_name = preg_replace("/^([a-zA-Z0-9].*\.)?([a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z.]{2,})$/", '$2', $ParsedURL['host']); 
        $domain_name = current(explode('.', $domain_name));
        print_r($domain_name); 

so above solution is satisfying only limited URLs ,it fails when TDL's are three or more in characters eg org.in , Again i am getting different results when there is no subdomain in url . Kindly help me out as i only want "domain" from Given URL

Try this regex /^\\.{1}.*\\.[a-zA-Z0-9]{2,3}(\\.[a-zA-Z0-9]{2,3})?$/ the only thing would be to strip the first '.' from the matched string: <?php substr($domain_name, 1);?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM