How to get a domain name without www, subdomain and com / net / org / etc.

I just need to get the domain name only from the url (without com and everything else):

I have:

http://www.subdomain.domain1.com/ www.subdomain.domain2.net subdomain.subdomain2.domain3.org/ http://domain4.com 

I want to get with PHP:

 domain1 domain2 domain3 domain4 
+5
source share
5 answers
 $url="http://www.seventymm.co.in/browse/buy-home-furnishing-bed-sheets-pack-of-3/2456/1/v2034/0/0/1/2/1/0/go"; echo get_domain($url); function get_domain($url) { $urlobj=parse_url($url); $domain=$urlobj['host']; if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[az\.]{2,6})$/i', $domain, $regs)) { return $regs['domain']; } return false; } 
+13
source

parse_url ()

with argument "PHP_URL_HOST"

then explode () in the "." and extract the penultimate element of the resulting array

+9
source

Based on Shyam's answer and using the concept suggested by Mark Baker to get the penultimate element of the namespace, I could suggest using a simpler regex using anchored lookup, as shown below:

 function get_domain($url) { $matches = []; preg_match('/[\w-]+(?=(?:\.\w{2,6}){1,2}(?:\/|$))/', $url, $matches); return $matches[0]; } 

Matches

  • names at https://www.names.co.uk/
  • domain1 at http://www.subdomain.domain1.com/
  • domain2 at www.subdomain.domain2.net
  • domain3 in subdomain.subdomain2.domain3.org/
  • domain4 at http://domain4.com
  • seventymm at http://www.seventymm.co.in/browse/buy-home-furnishing-bed-sheets-pack-of-3/2456/1/v2034/0/0/1/2/1/0/go
+2
source

Shyam's answer is the best. You just need to make sure that all URLs start with HTTP and add strtok to the end (see below). Here's another version using an array:

 function get_domain($url) { $urlobj = parse_url($url); $domain = $urlobj['host']; if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[az\.]{2,6})$/i', $domain, $regs)) { return $regs['domain']; } return false; } $urls = array( 'http://www.subdomain.domain1.com/', 'http://www.subdomain.domain2.net', 'http://subdomain.subdomain2.domain3.org/', 'http://domain4.com' ); // Using foreach + strtok to remove .[domain extensions] echo "<ul>"; foreach($urls as $url) { echo "<li>" . strtok(get_domain($url),'.') . "</li>"; } echo "</ul>"; 

This will output:

  • domain1
  • domain2
  • DOMAIN3
  • domain4
PS you can add strtok to a function as shown below:
 function get_domain($url) { $urlobj = parse_url($url); $domain = $urlobj['host']; if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[az\.]{2,6})$/i', $domain, $regs)) { return strtok($regs['domain'], '.'); } return false; } 

then

 $urls = array( 'http://www.subdomain.domain1.com/', 'http://www.subdomain.domain2.net', 'http://subdomain.subdomain2.domain3.org/', 'http://domain4.com' ); // Using foreach echo "<ul>"; foreach($urls as $url) { echo "<li>" . get_domain($url) . "</li>"; } echo "</ul>"; 

Minus ul / li:

  $urls = array( 'http://www.subdomain.domain1.com/', 'http://www.subdomain.domain2.net', 'http://subdomain.subdomain2.domain3.org/', 'http://domain4.com' ); // Using foreach foreach($urls as $url) { echo get_domain($url)."<br>"; } 

Conclusion: vacation rental domain1
domain2
DOMAIN3
domain4

0
source

To get the domain, you can use $_SERVER["SERVER_NAME"] .

Expand the line with the subdomain that you do not want to include.

Eg. subdomain.domain.com

 <?php $add=explode('subdomain.',$_SERVER["SERVER_NAME"])[1]; // $add = domain.com ?> 
-5
source

Source: https://habr.com/ru/post/1447511/


All Articles