Reputation: 8710
I know I can parse full url with parse_url in php, but my problem is when I have not full url ormat, how can I get domain name.
my urls are in 2 condtion sometimes in full path and sometimes in not full path,
as example
blahblah.com/ddddd/fffff/a.php?ddddd
http://blahblah.com/ddddd/fffff/a.php?ddddd
I want it return me blahbla.com
my url is not browser url,treat them like a variable not current url
Upvotes: 2
Views: 1343
Reputation: 885
Try this,
$uri = $_SERVER['REQUEST_URI']; // $uri == example.com/index
$exploded_uri = explode('/', $uri); //$exploded_uri == array('example.com','index')
$domain_name = $exploded_uri[0]; //$domain_name = 'example.com'
Upvotes: 0
Reputation: 68536
The right way to achieve this is by making use of $_SERVER['HTTP_HOST'];
Upvotes: 1
Reputation: 22721
Try this,
$url='blahblah.com/ddddd/fffff/a.php?ddddd';
$Urls = parse_url($url);
if(!isset($Urls['host'])){
$url = 'http://'.$url;
$Urls = parse_url($url);
}
echo $Urls['host'];
Upvotes: 0
Reputation: 2348
The best and official way to do it would be using parse_url()
But in your particular case you need a trick:
$url = 'http://'.preg_replace('!^https?://!i', '', $url);
echo parse_url( $url, PHP_URL_HOST );
Upvotes: 0
Reputation: 8710
tricky way :)
$url_p= parse_url($url);
if(!isset($url_p['scheme'])){
$url='http://'.$url;
$url_p=parse_url($url);
}
echo $url_p['host'];
Upvotes: 0
Reputation: 2007
use
echo $_SERVER['SERVER_NAME'];
or to remove 'www.' use
echo str_replace('www.', '', $_SERVER['SERVER_NAME']);
Upvotes: 1