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.
Input : subdomain.example.com Output : subdomain Input : contribute.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.
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.
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.
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.
- Subdomain in Flask | Python
- PHP Program for Cutting a Rod | DP-13
- PHP program to check for Anagram
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- PHP Program to find whether a no is power of two
- PHP Program for nth Catalan Number
- Hello World Program in Perl
- PHP Program for Subset Sum Problem | DP-25
- PHP Program for Egg Dropping Puzzle | DP-11
- PHP | Program to move (key,value) pair upwards or downwards
- PHP Program to Count set bits in an integer
- PHP Program for Largest Sum Contiguous Subarray
- PHP Program for Longest Palindromic Subsequence | DP-12
- PHP Program to print all permutations of a given string
- Difference between Algorithm, Pseudocode and Program
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.