Search Server Name

1

I would like your help for the following problem, I have this code.

function encontrarValor($url){

    $dominio = explode(".", parse_url($url, PHP_URL_HOST));
    return $dominio[0];

}

It allows you to return the host name almost perfectly. However, if the host address has www , it puts the server name as www . I would like your help to readjust this code so that even if url of the server has www , you can identify the server name perfectly.

    
asked by anonymous 10.06.2018 / 21:52

1 answer

0

In this case, I think it would solve this:

function encontrarValor($domain)
{
    if (preg_match('/(http(s)?:\/\/)?(www\.)?(.+)/', $domain, $matches)){
        return explode('/',$matches[4])[0];
    }
}
echo encontrarValor('http://www.teste.com.br/url-123');
echo "<br>";
echo encontrarValor('http://teste.com.br/url-123');

IDEONE sample

    
11.06.2018 / 14:46