summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Schneider2007-02-13 16:28:13 +0000
committerChristian Schneider2007-02-13 16:28:13 +0000
commitcc4ddb57a83e2c5c922f03e1151680b3e59b1263 (patch)
treec4b246616acfef66bf44935077bd64d5a31beea4
parent40c23e8e6ae2579fa279416b9f2fda5874b360a7 (diff)
downloaditools-cc4ddb57a83e2c5c922f03e1151680b3e59b1263.tar.gz
itools-cc4ddb57a83e2c5c922f03e1151680b3e59b1263.tar.bz2
itools-cc4ddb57a83e2c5c922f03e1151680b3e59b1263.zip
Added it_url::params (handles nested arrays) and use it in U() and search_url
-rw-r--r--html.class18
-rwxr-xr-xtests/html.t6
-rw-r--r--url.class34
3 files changed, 42 insertions, 16 deletions
diff --git a/html.class b/html.class
index ccacfea..d1fb5d2 100644
--- a/html.class
+++ b/html.class
@@ -315,23 +315,9 @@ function u(/* ... */)
$base = preg_replace('|\0|', '', $base);
$base = preg_replace('|[^\w.+!*(),:@&=/~$-]|e', 'urlencode("$0")', $base);
$base = preg_replace('|^(\w+:)?//[^/]*$|', '$0/', $base); # Add slash if absolute url without a path, e.g. http://gna.ch
- $queryparams = array();
+ $queryparams = it_url::params($params);
- foreach ($params as $key => $value)
- {
- if (is_array($value))
- {
- foreach ($value as $arrkey => $arrvalue)
- {
- if (strlen($arrvalue))
- $queryparams[] = urlencode($key) . "[" . urlencode($arrkey) . "]=" . it_url::encode($arrvalue);
- }
- }
- else if (strlen($value))
- $queryparams[] = urlencode($key) . "=" . it_url::encode($value);
- }
-
- return $base . ($queryparams ? ("?" . join("&", $queryparams)) : "");
+ return $base . ($queryparams ? "?$queryparams" : "");
}
diff --git a/tests/html.t b/tests/html.t
index 85f7818..071685e 100755
--- a/tests/html.t
+++ b/tests/html.t
@@ -89,5 +89,11 @@ is(
'it_html::sanitize tag soup'
);
+is(
+ U("/foo.html", 'bar' => array('gna' => 42, 'qux' => array('quux' => "<Zürich>", 'gnöp' => "fasel"))),
+ '/foo.html?bar[gna]=42&bar[qux][quux]=%3CZ%FCrich%3E&bar[qux][gn%F6p]=fasel',
+ 'U() with nested arrays'
+);
+
?>
diff --git a/url.class b/url.class
index 105a28b..f414449 100644
--- a/url.class
+++ b/url.class
@@ -527,6 +527,40 @@ function encode($str)
return strtr(urlencode($str), array("%2C"=>",", "%28"=>"(", "%29"=>")"));
}
+/**
+ * Create GET request from params, optionally only using given fields
+ * @param $params Array to take values from, usually $_GET
+ * @param $keys Keys to use; default: all
+ */
+function params($params, $keys = null)
+{
+ return join("&", it_url::_params($params, $keys));
+}
+
+function _params($params, $keys = null)
+{
+ $result = array();
+
+ if (!isset($keys))
+ $keys = array_keys($params);
+
+ foreach ($keys as $key)
+ {
+ if (is_array($params[$key]))
+ {
+ foreach (it_url::_params($params[$key]) as $value)
+ {
+ if (strlen($value))
+ $result[] = it::replace('^([^=\[]*)' => $key . '[$1]', $value);
+ }
+ }
+ else if (strlen($params[$key]))
+ $result[] = urlencode($key) . "=" . it_url::encode($params[$key]);
+ }
+
+ return $result;
+}
+
}
?>