How To Get Domain, Subdomain, TLD, CTLD & Path from URL In PHP


Recently for one of our project i was supposed to get Country Name, Sub Domain, Top Level Domain (TLD), Country Level Domain (CTLD) and Path from the given URL which will further be inserted into the database. Many functions which we used to test different scenarios, failed to perform on some logic or another. While searching for a better approach i ended up finding one answer in Stackoverflow by “Whatever Kitchen” which did the job perfectly. Here is a modified version of the function

Code Explaination

This function expects three parameters:

  1. URL
  2. Get What (default is everything)
  3. Output Format (default is an Array)

Calling the Function In Your App

You can call this function like url_information($myUrl, 'ALL', 'array'); within your application by sending those 3 parameters. Default output format will be an Array. Following are examples of how this function can be called to get the value.

I hope this function will help you get the information you need by providing proper parameters. Please feel free to suggest any further enhancements if you like. Subscribe our RSS to receive latest updates.

1 I like it
2 I don't like it