How to extract host from URL using regular expression in PHP

2 Answers

0 votes
$rawurl = "https://www.collectivesolver.com/36304/how-to-use-recursive-function-in-php";
$url = parse_url($rawurl);
print_r($url);

$domain = preg_replace('#^(?:.+?\\.)+(.+?\\.(?:co\\.uk|com|net))#', '$1', $url['host']);

echo $domain;



/*
run:

Array
(
    [scheme] => https
    [host] => www.collectivesolver.com
    [path] => /36304/how-to-use-recursive-function-in-php
)
www.collectivesolver.com

*/

 



answered Nov 28, 2020 by avibootz
0 votes
$rawurl = "https://doc.domain.co.uk/36304/782/xyz";
$url = parse_url($rawurl);
print_r($url);

$domain = preg_replace('#^(?:.+?\\.)+(.+?\\.(?:co\\.uk|com|net))#', '$1', $url['host']);

echo $domain;



/*
run:

Array
(
    [scheme] => https
    [host] => doc.domain.co.uk
    [path] => /36304/782/xyz
)
domain.co.uk

*/

 



answered Nov 28, 2020 by avibootz

Related questions

1 answer 217 views
2 answers 288 views
2 answers 437 views
437 views asked Mar 31, 2018 by avibootz
...