Example of Spatie/url in realodix/urlhub

File composer.json (link to Github)
{
    "require": {
        "php": "^8.0",
        //
        "spatie/url": "^2.0",
    },
}
File app/Services/UrlService.php (link to Github)
use Spatie\Url\Url as SpatieUrl;

class UrlService
{
    public function getDomain(string $url)
    {
        $url = SpatieUrl::fromString($url);

        return urlSanitize($url->getHost());
    }
}

Additional resources on spatie/url: