Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created February 25, 2019 16:00
Show Gist options
  • Save kobus1998/4fd685762445ddb380b296d364b99394 to your computer and use it in GitHub Desktop.
Save kobus1998/4fd685762445ddb380b296d364b99394 to your computer and use it in GitHub Desktop.
parse_url abstraction
<?php
namespace Http;
class Url
{
public $scheme;
public $host;
public $port;
public $user;
public $pass;
public $path;
public $query;
public $fragment;
public function __construct(string $url)
{
foreach(parse_url($url) as $key => $value) {
$this->{$key} = $value;
}
$query = $this->query;
$this->query = [];
foreach(explode('&', $query) as $param) {
$parts = explode('=', $param);
$this->query[$parts[0]] = $parts[1] ?? null;
}
}
}
$url = new \Http\Url( 'http://username:password@hostname:9090/path?arg=value#anchor');
print_r($url);
/*
Url Object
(
[scheme] => http
[host] => hostname
[port] => 9090
[user] => username
[pass] => password
[path] => /path
[query] => Array
(
[arg] => value
)
[fragment] => anchor
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment