Permalink
Browse files

Made usage of [] optional

git-svn-id: http://svn.php.net/repository/pear/packages/Net_URL/trunk@107132 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent ad094c1 commit 7491e13e599aa849cffaa6ca1cdd3d4d8c3c3b2a Richard Heyes committed Dec 7, 2002
Showing with 39 additions and 15 deletions.
  1. +38 −14 URL.php
  2. +1 −1 docs/example.php
View
52 URL.php
@@ -91,14 +91,20 @@ class Net_URL {
var $anchor;
/**
+ * Whether to use []
+ * @var bool
+ */
+ var $useBrackets;
+
+ /**
* Constructor
*
* Parses the given url and stores the various parts
* Defaults are used in certain cases
*
* @param $url The url
*/
- function Net_URL($url = null)
+ function Net_URL($url = null, $useBrackets = true)
{
global $HTTP_SERVER_VARS;
@@ -123,6 +129,7 @@ function Net_URL($url = null)
$this->path = $HTTP_SERVER_VARS['PHP_SELF'];
$this->querystring = isset($HTTP_SERVER_VARS['QUERY_STRING']) ? $this->_parseRawQuerystring($HTTP_SERVER_VARS['QUERY_STRING']) : null;
$this->anchor = '';
+ $this->useBrackets = $useBrackets;
// Parse the uri and store the various parts
if (!empty($url)) {
@@ -250,9 +257,9 @@ function getQueryString()
foreach ($this->querystring as $name => $value) {
if (is_array($value)) {
foreach ($value as $k => $v) {
- $querystring[] = sprintf('%s[%s]=%s', $name, $k, $v);
+ $querystring[] = $this->useBrackets ? sprintf('%s[%s]=%s', $name, $k, $v) : ($name . '=' . $v);
}
- } elseif ($value) {
+ } elseif (!is_null($value)) {
$querystring[] = $name . '=' . $value;
} else {
$querystring[] = $name;
@@ -275,20 +282,37 @@ function getQueryString()
*/
function _parseRawQuerystring($querystring)
{
- parse_str($querystring, $qs);
+ $querystring = rawurldecode($querystring);
+ $parts = preg_split('/&/', $querystring, -1, PREG_SPLIT_NO_EMPTY);
- foreach ($qs as $key => $value) {
- if (is_array($value)) {
- foreach ($value as $k => $v) {
- $value[$k] = rawurlencode($v);
+ $return = array();
+
+ foreach ($parts as $part) {
+ if (strpos($part, '=') !== false) {
+ $value = rawurlencode(substr($part, strpos($part, '=') + 1));
+ $key = substr($part, 0, strpos($part, '='));
+ } else {
+ $value = null;
+ $key = $part;
+ }
+
+ if (substr($key, -2) == '[]') {
+ $key = substr($key, 0, -2);
+ if (@!is_array($return[$key])) {
+ $return[$key] = array();
+ $return[$key][] = $value;
+ } else {
+ $return[$key][] = $value;
}
- $qs[$key] = $value;
- } elseif ($value) {
- $qs[$key] = rawurlencode($value);
+ } elseif (!$this->useBrackets AND !empty($return[$key])) {
+ $return[$key] = (array)$return[$key];
+ $return[$key][] = $value;
+ } else {
+ $return[$key] = $value;
}
- }
+ }
- return $qs;
- }
+ return $return;
+ }
}
?>
View
@@ -39,7 +39,7 @@
include('../URL.php');
- $url = &new Net_URL('http://www.example.com/foo/bar/index.php?jello');
+ $url = &new Net_URL('http://www.example.com/foo/bar/index.php?foo=bar');
?>
<html>
<body>

0 comments on commit 7491e13

Please sign in to comment.