Preg_match: get last two domain segments from url

2019-03-06 14:56发布

问题:

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?

回答1:

This piece of code:

$domain = 'http://www.php.net/index.html';
$url    = parse_url($domain);
$tokens = explode('.', $url['host']);

print_r($tokens);

Will give you this data:

Array
(
    [0] => www
    [1] => php
    [2] => net
)

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:

print_r($url);

$url array contains all necessary details:

Array
(
    [scheme] => http
    [host] => www.php.net
    [path] => /index.html
)