How to get the host name in URLs?

2

How to get the name of the host from a URL like Mega, 4shared among others with PHP?

In case you would like to know having this URL as an example:

  

link

How can I get from this URL only the word MEGA discarding everything else?

I found this type of code on the internet which takes the host name only does not do what I want because it still leaves the www and the domain I just want to leave the host name.

$host = $_SERVER['HTTP_HOST'];
echo $host;
    
asked by anonymous 21.01.2015 / 19:17

2 answers

3

You can get this value by manipulating HTML, see:

$contextOptions = array(
    "ssl"=>array(
        "verify_peer" => false,
        "verify_peer_name" => false,
    ),
); 

$url = file_get_contents("https://mega.co.nz/#!4090kJrY!LZiZBgsOo_Gg1sLnykZLHsUThAC9oaPRG3---0gD92Y", false, stream_context_create($contextOptions));

$dom = new DOMDocument();
$dom->loadHTML($url);

$title = $dom->getElementsByTagName('title');

print($title->item(0)->nodeValue . "\n"); // MEGA

Demo

    
21.01.2015 / 19:30
3

Maybe what you want is this:

<?php
$url = 'https://mega.co.nz/#!4090kJrY!LZiZBgsOo_Gg1sLnykZLHsUThAC9oaPRG3---0gD92Y';
$array_url = parse_url($url);
print_r($array_url);
echo parse_url($url, PHP_URL_HOST) . "\n"; //pega o HOST
echo $array_url['host'] . "\n"; //aqui pega só o HOST também.
$dominio = explode(".", parse_url($url, PHP_URL_HOST));
echo $dominio[0];
?>

See running on ideone .

Function documentation .

    
21.01.2015 / 19:30