The domain is not the localhost
.
If the user types a link, for example, http://exemplo.com/pasta/pasta2/past/ficheiro.php
, I want PHP to return only this part of url exemplo.com
The domain is not the localhost
.
If the user types a link, for example, http://exemplo.com/pasta/pasta2/past/ficheiro.php
, I want PHP to return only this part of url exemplo.com
PHP already has a function that separates all the elements of a URL.
It's just the parse_url
:
$url = 'http://exemplo.com/pasta/pasta2/past/ficheiro.php';
$elementos = parse_url($url);
echo $elementos['host']; // 'exemplo.com'
If you want a specific part, use the second parameter:
echo parse_url($url, PHP_URL_HOST); // 'exemplo.com'
See working at IDEONE .
These are the options for the second parameter:
PHP_URL_SCHEME // retorna string - ex: http
PHP_URL_HOST // retorna string - ex: site.com
PHP_URL_PORT // retorna inteiro - ex: 443
PHP_URL_USER // retorna string - ex: admin
PHP_URL_PASS // retorna string - ex: swordfish
PHP_URL_PATH // retorna string - ex: /blog
PHP_URL_QUERY // retorna string - ex: secao=esportes
PHP_URL_FRAGMENT // retorna string - ex: ancora2
If you want to use more than one part, better get the entire array:
$partes = parse_url($url); // retorna array associativo
and use all the parts that matter:
echo $partes['scheme'].'://'.$partes['host'].$partes['path'];
More details in the manual: