Organizational Research By

Surprising Reserch Topic

Question:PHP:Getting the domain name out of a URL?



asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
0 votes
42 views



Related Hot Questions

2 Answers

0 votes

 

// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
$host = $matches[1];
 
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
 
The above example will output:
 
domain name is: example.com
 

 

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

 

// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
$host = $matches[1];
 
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
 
The above example will output:
 
domain name is: example.com
 

 

answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...