rlvendramini / url-parser
A URL parser to edit and rebuild URL strings
v1.0
2021-02-12 01:42 UTC
Requires
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-12-17 20:33:34 UTC
README
Class that implements more control over an URL in PHP, like a Javascript URL class
Instalation
composer require rlvendramini/url-parser
Setup
If you already have a vendor library in your project, you might have the following line. If not, just add:
require __DIR__ . '/vendor/autoload.php';
Usage
Instantiate from a string
$url = URLParser::fromString($string);
then you can get query string params
$url = URLParser::fromString('https://foo.bar/home?param=value'); $url->getParam('param'); // value
and set new params or overwrite existing ones
$url = URLParser::fromString('https://foo.bar/home?param=value'); $url->setParam('param', 'super value'); // super+value $url->setParam(' #amazing param~', 'amazing value'); // amazing+value $url->getParam('param'); // super+value $url->getParam('amazing_param'); // amazing+value
and finally, you can get modified url as a string
$url = URLParser::fromString('https://foo.bar/home?param=value'); $url->setParam('param', 'super value'); // super+value $url->setParam(' #amazing param~', 'amazing value'); // amazing+value $url->getParam('param'); // super+value $url->getParam('amazing_param'); // amazing+value $url->toString() // https://foo.bar/home?param=super+value&amazing_param=amazing+value