Created
February 25, 2019 16:00
-
-
Save kobus1998/4fd685762445ddb380b296d364b99394 to your computer and use it in GitHub Desktop.
parse_url abstraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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