diff --git a/src/Purl/Fragment.php b/src/Purl/Fragment.php index d973ecd..5b415d4 100644 --- a/src/Purl/Fragment.php +++ b/src/Purl/Fragment.php @@ -7,6 +7,8 @@ use function array_merge; use function parse_url; use function sprintf; +use function strpos; +use function substr; /** * Fragment represents the part of a Url after the hashmark (#). @@ -113,7 +115,17 @@ public function __toString() : string protected function doInitialize() : void { if ($this->fragment !== null) { - $this->data = array_merge($this->data, parse_url($this->fragment)); + $pos = strpos($this->fragment, ':', 1); + $colon = ''; + if ($pos !== false) { + $colon = substr($this->fragment, $pos); + } + $data = parse_url($this->fragment); + if ($colon !== '') { + $data = ['path' => $this->fragment]; + } + + $this->data = array_merge($this->data, $data); } foreach ($this->data as $key => $value) { diff --git a/tests/Purl/Test/UrlTest.php b/tests/Purl/Test/UrlTest.php index 6344756..6fe671b 100644 --- a/tests/Purl/Test/UrlTest.php +++ b/tests/Purl/Test/UrlTest.php @@ -362,6 +362,10 @@ public function testRelativeUrl() : void $url = new Url('/events'); $url->query->set('param1', 'value1'); $this->assertEquals('/events?param1=value1', (string) $url); + + // test fragment with colon + $url = new Url('http://example.com/#hello:123'); + $this->assertEquals('http://example.com/#hello:123', (string) $url); } }