There is an example on php.net how to get last two domain segments in two steps:
<?php
//get host name from URL
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
$host = $matches[2];
// get last two segments of host name
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain name is: {$matches[0]}\n";
/* Output is php.net */
?>
But how can I do it in one step, using only one preg_match expression?
This piece of code:
Will give you this data:
I believe there is no need for regexs as far as it's very hard to properly parse URL with them. From resulting $tokens array you can extract any part of host name easily.
Update:
$url array contains all necessary details: