diff options
author | Christian Schneider | 2007-02-13 16:28:13 +0000 |
---|---|---|
committer | Christian Schneider | 2007-02-13 16:28:13 +0000 |
commit | cc4ddb57a83e2c5c922f03e1151680b3e59b1263 (patch) | |
tree | c4b246616acfef66bf44935077bd64d5a31beea4 | |
parent | 40c23e8e6ae2579fa279416b9f2fda5874b360a7 (diff) | |
download | itools-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.class | 18 | ||||
-rwxr-xr-x | tests/html.t | 6 | ||||
-rw-r--r-- | url.class | 34 |
3 files changed, 42 insertions, 16 deletions
@@ -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' +); + ?> @@ -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; +} + } ?> |