Split domain name into sld and tld – php script

A series of alphanumeric strings that are separated by periods, such as www.shariarbd.com, that is an address of a computer network connection and that identifies the owner of the address.

Also, Domain names are used to identify one or more IP addresses. For example, the domain name facebook.com represents about a dozen IP addresses. Domain names are used in URLs to identify particular Web pages. For example, in the URL http://www.hotmobiapp.com/index.html, the domain name is hotmobiapp.com.

What is SLD

SLD (Second-Level Domain): A second-level domain (SLD) is the portion of a URL (Uniform Resource Locator)  that identifies the specific and unique administrative owner associated with an IP address (Internet Protocol address). The second-level domain name includes the top-level domain (top-level domain) name. For example, in: hotmobiapp.com

"hotmobiapp" is a second-level domain. "hotmobiapp.com" is a second-level domain name (and includes the top-level domain name of "com"). Second-level domains can be divided into further domain levels. These subdomains sometimes represent different computer servers within different departments.

What is TLD

TLD (Top Level Domain): A top-level domain (TLD) is one of the domains at the highest level in the hierarchical Domain Name System of the Internet.  The top-level domain names are installed in the root zone of the name space. For all domains in lower levels, it is the last part of the domain name, that is, the last label of a fully qualified domain name. For example, in the domain name www.hotmobiapp.com, the top-level domain is .com

Well, then what was index.html? or some time /someting/blog.php ? Well, after a domain name, others are either file name or directory name.

Well, Now we know about Domain Name, SLD and TLD. And now, we will see how to split a domain name into SLD and TLD

// Following are all allowed domain extension.  
$knownExtensions = array(

$regexp = "/".str_replace(".", "\\.", join("|", $knownExtensions))."/";
$theDomain = preg_replace($regexp, "",$originalDomain);

$regexp = "/$theDomain./"; 
$extension = preg_replace($regexp, "", $originalDomain); //To find tld
$regexp = "/www./"; 
$extension = preg_replace($regexp, "", $extension); //This is to remove www. form domain name if it is in it.

$SLD=$theDomain; // output as SLD
$TLD=$extension; // output as TLD

How to use it?



the variable $originalDomain is getting the original domain name and see on the bottom of the code


The above two variable $SLD and $TLD are represent the SLD and TLD as the separated value of the domain name.

2 thoughts on “Split domain name into sld and tld – php script”

Leave a Reply

Your email address will not be published. Required fields are marked *