How to get the host name in Urls?

Asked

Viewed 4,158 times

3

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

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

https://mega.co.nz/#! 4090kJrY! Lzizbgsoo_gg1slnykzlhsuthac9oaprg3--0gD92Y

How can I get from this URL only the word MEGA discarding all the rest?

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

$host = $_SERVER['HTTP_HOST'];
echo $host;
  • 1

    Then explain better what you want. Separate the text further to make it clearer, give some example, show what you did and what is wrong. Make sure you are using the correct terms if you are citing the situation that the information is obtained properly. http://php.net/manual/en/reserved.variables.server.php

  • ready I hope now you understand

2 answers

4

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];
?>

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Job documentation.

  • in the case of this code is placed this URL https://mega.co.nz/#! 4090kJrY! Lzizbgsoo_gg1slnykzlhsuthac9oaprg3--0gD92Y will appear only the word mega in echo ? And if you have www how would it look ?

  • What I had posted wouldn’t just take this, I made a modification to pick it up. But note that it’s a pretty rough shape. It works in a very specific situation. It’s very difficult to get what you want. There are many rules that need to be observed. It would have to have a table of all Tlds to check if what comes before one of them is the domain you want. I don’t have the RFC here but I don’t think even this is enough to guarantee the result.

  • OK I already understood having in case the reference names could make a search with precision right ? if I put them in SQL it would be possible to do this by searching a comparison between the name and the URL taking only the specific name.

  • Yes, with enough information you could.

  • Thank you for your help.

2


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

  • in the case of this code is placed this URL https://mega.co.nz/#! 4090kJrY! Lzizbgsoo_gg1slnykzlhsuthac9oaprg3--0gD92Y will appear only the word mega in print ?

  • Thanks for all the help the 2 code worked the way I wanted in case it checks the existence of the host first right ? if there is no false value ne and concludes by putting only the name of the host in the mega case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.