2013-04-16 15 views
22

che qualcuno entra in un URL simile a questo:Get nome di dominio da URL completo

http://i.imgur.com/a/b/c?query=value&query2=value 

E voglio tornare: imgur.com

non i.imgur.com

Questo è il codice che ho in questo momento

$sourceUrl = parse_url($url); 
$sourceUrl = $sourceUrl['host']; 

Ma restituisce i.imgur.com

+0

cura da condividere ?? – ramo

+0

come su quello che inizia "Ecco un metodo per ottenere il nome REALE di un dominio". –

+0

Vedi: http://stackoverflow.com/questions/288810/get-the-subdomain-from-a-url – duskwuff

risposta

61

Controllare il codice qui sotto, dovrebbe fare il lavoro bene.

<?php 

function get_domain($url) 
{ 
    $pieces = parse_url($url); 
    $domain = isset($pieces['host']) ? $pieces['host'] : $pieces['path']; 
    if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { 
    return $regs['domain']; 
    } 
    return false; 
} 

print get_domain("http://mail.somedomain.co.uk"); // outputs 'somedomain.co.uk' 

?> 
+0

Questo funziona benissimo! Grazie! Segnalo come la migliore risposta. – ramo

+3

Questo è un * piccolo * difficile. Finirà per restituire risultati errati per sottodomini di ccTLD di primo livello, come 'blah.blah.de'. Ma non c'è modo di aggirare questo senza usare l'elenco dei suffissi pubblici. – duskwuff

+0

Questo non funzionerà per 'www.dominio.com' - parse_url() necessita di un protocollo (http: //) per definire un host, altrimenti è solo percorso. Richiede un condizionale secondario sotto il primo: '$ dominio = (vuoto ($ dominio) && isset ($ pezzi ['percorso']))? $ pieces ['path']: $ domain; ' –

-1
 if(substr_count($original_url, 'http://')) { 
    if(substr_count($original_url, 'www.')) { 
     // url style would be 'http://www.abc.xxx/page?param' or http://www.abc.xxx.xx/page?param 
     // extract 'abc' 
     $temp = explode('.', $original_url); 

     $store_url = $temp[1]; 
     // now 
     // $temp[2] = xxx or xxx/page?param 
     // $temp[3] = null or xx/page?param 

     //if ($temp[3] == null) { // then we are sure that $temp[2]== "xxx/page?param" 
        if(sizeof($temp) > 3) { 
      // extract "xxx" from "xxx/page?param" and append to store url so it will be "abc.xxx" 
      $temp = explode('/',$temp[2]); 
      $store_url .= '.'.$temp[0]; 
     } 
     else { 
      // then we are sure that $temp[2]== "xxx" and then $temp[3] == "xx/page?param" 
      //     or $temp[2]== xxx/page?stripped-link from second dot(.) 
      if(substr_count($temp[2], '/')) { // in case $temp[2]== xxx/page?stripped-link from second dot(.) 
       // extract "xxx" from "xxx/page?stripped-link" and appent to store url so it will be "abc.xxx" 
       $temp = explode('/',$temp[2]); 
       $store_url .= '.'.$temp[0]; // "abc".="xxx" ==> abc.xxx 
      } 
      else { // in case $temp[2]== "xxx" and then $temp[3] == "xx/page?param" 
       $store_url .= '.'.$temp[2]; // "abc".="xxx" ==> abc.xxx 
       // extract "xx" from "xx/page?param" and appent to store url so it will be "abc.xxx.xx" 
       $temp = explode('/',$temp[3]); 
       if(strlen($temp[0])==2) { 
        $store_url .= '.'.$temp[0]; 
       } 
      } 
     } 
    } 
    else { 
     // url style would be 'http://abc.xxx/page?param' or 'http://abc.xxx.xx/page?param' 
     // remove 'http://' 
     $temp = substr($original_url, 7); 
     // now temp would be either 'abc.xxx/page?param' or 'abc.xxx.xx/page?param' 
     // explode with '/' 
     $temp = explode('/', $temp); 
     $store_url = $temp[0]; 
    } 
} 
else if(substr_count($original_url, 'www.')) { 
    // url style would be 'www.abc.xxx/page?param' or 'www.abc.xxx.xx/page?param' 
    // remove 'www.' 
    $temp = substr($original_url, 4); 
    // now, $temp would be either "abc.xxx/page?param" or "abc.xxx.xx/page?param" 
    // explode with '/' 
    $temp = explode('/', $temp); 
    $store_url = $temp[0]; 
} 
else { 
    // url style would be 'abc.xxx/page?param' or 'abc.xxx.xx/page?param' 
    //explode with '/' 
    $temp = explode('/', $original_url); 
    $store_url = $temp[0]; 
} 
+0

Ummm ... che cos'è? –

+0

è la funzione per ottenere il nome host dall'URL.l'url originale è il tuo url e $ store_url restituisce l'URL dell'host. –

-6

uso questo:

$uri = "$_SERVER[REQUEST_URI]";<br> 
print($uri); 

Esempio:

http://exemple.com/?directory<br> 
Result: 
/?diretory 

Il comando ottenere il dominio di directory e non.

+0

La domanda si riferisce a un URL inserito da un utente, non all'indirizzo che sta visitando. – robmcvey

-5

Se si desidera solo il nome del dominio, provare quanto segue:

$domain = $_SERVER['SERVER_NAME']; 

echo $domain; 
5

avete bisogno del pacchetto che l'utilizzo Public Suffix List. Sì, puoi usare le funzioni di stringa attorno a parse_url() o regex, ma produrranno risultati non corretti in URL complessi.

Raccomando TLDExtract per l'analisi del dominio, ecco il codice di esempio:

$url = 'http://i.imgur.com/a/b/c?query=value&query2=value'; 

parse_url($url, PHP_URL_HOST); // will return 'i.imgur.com' 

$extract = new LayerShifter\TLDExtract\Extract(); 
$result = $extract->parse($url); 
$result->getFullHost(); // will return 'i.imgur.com' 
$result->getSubdomain(); // will return 'i' 
$result->getRegistrableDomain(); // will return 'imgur.com' 
$result->getSuffix(); // will return 'com' 
1

ho trovato una libreria molto utile utilizzando publicsuffix.org, PHP Domain Parser è un parser di dominio basato list pubblica suffisso implementato in PHP .

https://github.com/jeremykendall/php-domain-parser

<?php 
// this will do the job 

require_once '../vendor/autoload.php'; 

$pslManager = new Pdp\PublicSuffixListManager(); 
$parser = new Pdp\Parser($pslManager->getList()); 
var_dump($parser->getRegistrableDomain('www.scottwills.co.uk')); 
?> 

string (16) "scottwills.co.uk"