From 2e249fccd8e5673ce6d12864fc737b938099b199 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 8 Jun 2010 14:21:35 -0600 Subject: [PATCH] Add support for URL anchors to Horde_Url. --- framework/Url/lib/Horde/Url.php | 24 ++++++++++++++++++++---- framework/Url/package.xml | 3 ++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/framework/Url/lib/Horde/Url.php b/framework/Url/lib/Horde/Url.php index 427263b4e..641b7520c 100644 --- a/framework/Url/lib/Horde/Url.php +++ b/framework/Url/lib/Horde/Url.php @@ -56,6 +56,13 @@ class Horde_Url public $pathInfo; /** + * The anchor string. + * + * @var string + */ + public $anchor = ''; + + /** * Constructor. * * @param string $url The basic URL, with or without query parameters. @@ -64,17 +71,23 @@ class Horde_Url */ public function __construct($url, $raw = null) { - /* @todo Remove if all code has been moved to Horde_Url. */ if ($url instanceof Horde_Url) { - $this->url = $url->url; + $this->anchor = $url->anchor; $this->parameters = $url->parameters; $this->pathInfo = $url->pathInfo; $this->raw = is_null($raw) ? $url->raw : $raw; + $this->url = $url->url; return; } - if (strpos($url, '?') !== false) { - list($url, $query) = explode('?', $url); + if (($pos = strrpos($url, '#')) !== false) { + $this->anchor = urldecode(substr($url, $pos + 1)); + $url = substr($url, 0, $pos); + } + + if (($pos = strrpos($url, '?')) !== false) { + $query = substr($url, $pos + 1); + $url = substr($url, 0, $pos); /* Check if the argument separator has been already * htmlentities-ized in the URL. */ @@ -208,6 +221,9 @@ class Horde_Url if (count($url_params)) { $url .= '?' . implode($raw ? '&' : '&', $url_params); } + if ($this->anchor) { + $url .= '#' . rawurlencode($this->anchor); + } return $url; } diff --git a/framework/Url/package.xml b/framework/Url/package.xml index a7c0bee1a..ab63d5167 100644 --- a/framework/Url/package.xml +++ b/framework/Url/package.xml @@ -29,7 +29,8 @@ http://pear.php.net/dtd/package-2.0.xsd"> beta LGPL - * Added Horde_Url::uriB64Encode() and Horde_Url::uriB64Decode(). + * Add support for URL anchors. + * Added Horde_Url::uriB64Encode() and Horde_Url::uriB64Decode(). * Initial package. -- 2.11.0