An easy-to-use PHP helper (and Laravel Facade) to parse out different parts of a URL
- PHP 8.0+
composer require geoffreyrose/url-helper
use UrlHelper\UrlHelper;
...
$urlHelper = new UrlHelper();
Laravel uses Package Auto-Discovery, which doesn't require you to manually add the ServiceProvider and Facade.
$rootHostname = URLHelper::getRootHostname('https://github.com/geoffreyrose/url-helper/')
Note all examples below use Plain PHP (use UrlHelper\UrlHelper) but can be swapped with Laravel Facade (URLHelper)
isValidDomainName(string $domain): bool
$urlHelper = new UrlHelper();
$urlHelper->isValidDomainName('https://example.com'); // false
$urlHelper->isValidDomainName('example.com'); // true
$urlHelper->isValidDomainName('Frodo Baggins'); // false
getHostname(string $url): ?string
$urlHelper = new UrlHelper();
$urlHelper->getHostname('https://example.com'); // example.com
$urlHelper->getHostname('https://www.example.com')); // www.example.com
$urlHelper->getHostname('Bilbo Baggins'); // null
getScheme(string $url): ?string
$urlHelper = new UrlHelper();
$urlHelper->getScheme('https://example.com'); // https
$urlHelper->getScheme('example.com'); // null
$urlHelper->getScheme('ftp://example.com'); // ftp
$urlHelper->getScheme('Dark Lord Sauron'); // null
getRootHostname(string $url): ?string
$urlHelper = new UrlHelper();
$urlHelper->getRootHostname('https://example.com'); // example.com
$urlHelper->getRootHostname('https://www.example.com')); // example.com
$urlHelper->getRootHostname('Samwise Gamge'); // null
getUrlWithoutScheme(string $url, bool $trimTrailingSlash=false): ?string
$urlHelper = new UrlHelper();
$urlHelper->getUrlWithoutScheme('https://example.com'); // example.com
$urlHelper->getUrlWithoutScheme('https://example.com/', true); // example.com
$urlHelper->getUrlWithoutScheme('https://example.com/test/?abc=123', true); // example.com/test?abc=123
$urlHelper->getUrlWithoutScheme('https://www.example.com')); // www.example.com
$urlHelper->getUrlWithoutScheme('Peregrin Took'); // null
getValidURL(string $url): ?string
$urlHelper = new UrlHelper();
$urlHelper->getValidURL('https://example.com'); // https://example.com
$urlHelper->getValidURL('https://www.example.com'); // https://www.example.com
$urlHelper->getValidURL('https://example.com/test'); // https://example.com/test
$urlHelper->getValidURL('example.com'); // null
$urlHelper->getValidURL('Merry Brandybuck')); // null
convertAndroidAppToHttps(string $url): ?string
$urlHelper = new UrlHelper();
$urlHelper->convertAndroidAppToHttps('android-app://com.example'); // https://example.com
$urlHelper->convertAndroidAppToHttps('android-app://example.com'); // https://example.com
$urlHelper->convertAndroidAppToHttps('Dark Lord Sauron'); // null
getPathname(string $url): ?string
$urlHelper = new UrlHelper();
$urlHelper->getPathname('https://example.com/path/to/somewhere'); // /path/to/somewhere
$urlHelper->getPathname('https://example.com'); // /
$urlHelper->getPathname('https://example.com/test/abc?test=12345')); // /test/abc
getParameters(string $url): ?array
$urlHelper = new UrlHelper();
$urlHelper->getParameters('https://example.com/path/to/somewhere?test=12345&test2=abc'); // ['test' => '12345', 'test2' => 'abc']
$urlHelper->getParameters('https://example.com'); // null
$urlHelper->getPathname('Dark Lord Sauron'); // null
./vendor/bin/phpunit
// or with coverage
XDEBUG_MODE=coverage ./vendor/bin/phpunit