\sFire\Utils\URLParser is a handy utility for parsing URL's and parsing URL queries also known as GET parameters.
sFire URL Parser gives you quick access to all the parts of a URL. Below are methods to parse a URL and gain access to the parts.
$url = new URLParser('https://user:pass@example.com/docs/latest?page=2#filter');
$url -> getScheme(); //Output: "https"
$url -> getUser(); //Output: "user"
$url -> getPassword(); //Output: "pass"
$url -> getHost(); //Output: "example.com"
$url -> getPath(); //Output: "/docs/latest"
$url -> getQuery(); //Output: "page=2"
$url -> getFragment(); //Output: "filter"
If you are only interested in the URL until the "path", you can use the generate method to generate the URL until the URL reaches the path value. The default value is to return the whole URL (UNTIL_FRAGMENT).
$url = new URLParser('https://example.com/docs/latest?page=2#filter');
$url -> generate(URLParser :: UNTIL_HOST); //Outputs "https://example.com"
| Option | Value |
|---|---|
| UNTIL_SCHEME | scheme |
| UNTIL_HOST | host |
| UNTIL_USER | user |
| UNTIL_PASSWORD | pass |
| UNTIL_PATH | path |
| UNTIL_QUERY | query |
| UNTIL_FRAGMENT | fragment |
Parses the query string and converts it into an Array.
$url = new URLParser('https://example.com/?page=2&sort=price');
$query = $url -> parseQuery();
print_r($query);
//Outputs:
Array
(
[page] => 2
[sort] => price
)
To return the URL parsed, you can use the parseURL method. This will return an Object with all the chunks of a URL.
$url = new URLParser('https://user:pass@example.com/docs/latest?page=2#filter');
$parsed = $url -> parseUrl();
print_r($parsed);
//Outputs:
stdClass Object
(
[scheme] => https
[host] => example.com
[user] => user
[pass] => pass
[path] => /docs/latest
[query] => page=2
[fragment] => filter
)