cvs: pear /Net_URL URL.php /Net_URL/docs example.php

From: Date: Sat, 07 Dec 2002 14:12:32 +0000
Subject: cvs: pear /Net_URL URL.php /Net_URL/docs example.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-7763@lists.php.net to get a copy of this message
richard Sat Dec 7 09:12:32 2002 EDT Modified files: /pear/Net_URL URL.php /pear/Net_URL/docs example.php Log: Made usage of [] optional Index: pear/Net_URL/URL.php diff -u pear/Net_URL/URL.php:1.19 pear/Net_URL/URL.php:1.20 --- pear/Net_URL/URL.php:1.19 Thu Nov 14 08:28:01 2002 +++ pear/Net_URL/URL.php Sat Dec 7 09:12:32 2002 @@ -91,6 +91,12 @@ var $anchor; /** + * Whether to use [] + * @var bool + */ + var $useBrackets; + + /** * Constructor * * Parses the given url and stores the various parts @@ -98,7 +104,7 @@ * * @param $url The url */ - function Net_URL($url = null) + function Net_URL($url = null, $useBrackets = true) { global $HTTP_SERVER_VARS; @@ -123,6 +129,7 @@ $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 @@ 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 _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; + } } ?> Index: pear/Net_URL/docs/example.php diff -u pear/Net_URL/docs/example.php:1.3 pear/Net_URL/docs/example.php:1.4 --- pear/Net_URL/docs/example.php:1.3 Thu Nov 14 08:30:47 2002 +++ pear/Net_URL/docs/example.php Sat Dec 7 09:12:32 2002 @@ -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>

« previous php.pear.cvs (#7763) next »