Open In App

Program to get the subdomain of a URL using PHP

Last Updated : 11 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Given a URL and the task is to get the sub-domain from the given URL. Use explode() function which breaks a string into array or preg_split() function which splits the given string into array by Regular Expression.

Examples:

Input : subdomain.example.com
Output : subdomain

Input : write.geeksforgeeks.org
Output : contribute

Method 1: PHP | explode() Function: The explode() function is an inbuilt function in PHP which is used to split a string in different strings. The explode() function splits a string based on the string delimiters, i.e. it splits the string wherever the delimiter character occurs. This functions returns an array containing the strings formed by splitting the original string.

Syntax:

array explode( separator, OriginalString, NoOfElements )

Use explode() function to get the sub-domain from URL. Getting the sub-domain using explode() function is easier than getting sub-domain using preg_split() function.

Example 1:




<?php
  
$URL = "subdomain.example.com";
  
// Split string into array
$arr = explode('.', $URL);
  
// Get the first element of array
$subdomain = $arr[0];
  
echo $subdomain;
  
?>


Output:

subdomain

Example 2:




<?php
  
$URL = "write.geeksforgeeks.org";
  
// Split string into array
$arr = explode('.', $URL);
  
// Get the first element of array
$subdomain = $arr[0];
echo $subdomain;
  
?>


Output:

contribute

Method 2: PHP | preg_split() Function: The preg_split() function is an inbuilt function in PHP which is used to convert the given string into an array by Regular Expression. If matching fails, an array with a single element containing the input string will be returned.

Syntax:

array preg_split( $pattern, $subject, $limit, $flag )

Use preg_split() function to get the sub-domain from URL. Passing the Regular Expression as parameter to the function and it split the URL.

Example 1:




<?php
  
$URL = "write.geeksforgeeks.org";
  
// Escape '.' as special symbol for regular expression.
$arr = preg_split('[\.]', $URL); 
  
$subdomain = $arr[0];
  
echo $subdomain;
?>


Output:

contribute

Example 2:




<?php
  
$URL = "ide.geeksforgeeks.org";
  
// Escape '.' as special symbol for regular expression.
$arr = preg_split('[\.]', $URL); 
  
$subdomain = $arr[0];
  
echo $subdomain;
  
?>


Output:

ide


Previous Article
Next Article

Similar Reads

Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
Subdomain in Flask | Python
Prerequisite: Introduction to Flask In this article, we will learn how to setup subdomains in Flask. But first, let's go through the basic like what is DNS and subdomains. Domain Name System (DNS): The Domain Name System (DNS) is a hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or
3 min read
Massc - Subdomain Scanner Tool Designed in JavaScript
Subdomain enumeration is the process of finding valid (resolvable) subdomains for one or more domain(s). The general system is to use a dictionary of common names, trying to resolve them. These detected subdomains can reveal crucial information about the target, which might not be present in the main domain. Massc tool is a Nodejs language-based to
3 min read
Subdomain takeover from scratch to advance
Sub-domain Takeover : Sub-domain takeover is a common and most popular vulnerability. If you are not aware of such kind of vulnerability, you can understand it as a class of security issues where the intention of an attacker is to take control of an organization's sub-domain via cloud services. Sub-domain takeover vulnerability sometimes may lead t
2 min read
How to Setup a Subdomain on Your WordPress Website ?
Subdomains are like branches of a tree, helping to organize and enhance the user experience on a website. They act as separate spaces, offering distinct functions and content. For instance, consider the website xyz.com. Alongside its primary purpose, it creates a subdomain called blog.xyz.com, forming a distinct online space. Both xyz.com and blog.
7 min read
What is URL Locator & what are common schemes used for the URL in HTML ?
Every HTML document on the internet has a unique URL. URL stands for Uniform Resource Locator. The URL is the HTML document's address on the internet. Any file that can be accessed via the internet has a URL, including images, JavaScript files, CSS files, Flash files, and so on. To view an HTML document in a browser, enter the URL of the document i
3 min read
Get the full URL in PHP
In this article, we will see how to get the complete URL of currently running pages using PHP, along with understanding their implementation through the examples. $_SERVER is a super global variable in PHP that contains the details related to the header, paths, and script locations. The status of HTTPS will be saved in the Global variable $_SERVER[
3 min read
How to get parameters from a URL string in PHP?
The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it. It parse an URL and return an associative array which contains its various c
2 min read
How to get complete current page URL in PHP ?
In PHP. there is a superglobal variable that can provide you with the URL of the current working page. The $_SERVER is an inbuilt variable that can access the page URL, after that, you have to check that the URL is following which type of HTTP protocol. What is Superglobal? Superglobals are already defined variables by the PHP engine which can be u
1 min read
How to access data sent through URL with GET method in PHP ?
On a website, we often use forms to collect data, login/signup boxes for users, a search box to search through a web page, etc. So input fields are used to make the website interactive and to collect data. But we know that HTML can not store the data in a database so for that we use a backend scripting language like PHP. URLs structure for GET meth
2 min read