sFire PHP Framework

URL Parser

\sFire\Utils\URLParser is a handy utility for parsing URL's and parsing URL queries also known as GET parameters.

In this section we will handle:

  • Parsing a URL
  • Generating a URL
  • Parsing the Query string
  • Return the parsed URL

Parsing a URL

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"

Generating URL

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"
The available options are:
Option Value
UNTIL_SCHEME scheme
UNTIL_HOST host
UNTIL_USER user
UNTIL_PASSWORD pass
UNTIL_PATH path
UNTIL_QUERY query
UNTIL_FRAGMENT fragment

Parsing the Query string

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
)

Return the parsed URL

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
)