geoffreyrose / url-helper
A URL helper to parse out different parts of a URL
Fund package maintenance!
geoffreyrose
Requires
- php: ^8.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.5
- phpunit/phpunit: ^9.0|^10.0|^11.0
README
PHP URL Helper + Laravel Facade
An easy-to-use PHP helper (and Laravel Facade) to parse out different parts of a URL
Requirements
- PHP 8.0+
Usage
Install
composer require geoffreyrose/url-helper
With Plain PHP
use UrlHelper\UrlHelper; ... $urlHelper = new UrlHelper();
With Laravel Facade
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/')
Methods
Note all examples below use Plain PHP (use UrlHelper\UrlHelper) but can be swapped with Laravel Facade (URLHelper)
isValidDomainName()
isValidDomainName(string $domain): bool
$urlHelper = new UrlHelper();
$urlHelper->isValidDomainName('https://example.com'); // false
$urlHelper->isValidDomainName('example.com'); // true
$urlHelper->isValidDomainName('Frodo Baggins'); // false
getHostname()
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()
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()
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()
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()
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()
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()
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()
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
Run Tests
./vendor/bin/phpunit
// or with coverage
XDEBUG_MODE=coverage ./vendor/bin/phpunit