source: branches/version-2_5-dev/data/module/Compat/Compat/Function/http_build_query.php @ 20116

Revision 20116, 3.6 KB checked in by nanasess, 13 years ago (diff)
  • svn properties を再設定
  • 再設定用のスクリプト追加
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?PHP
2// +----------------------------------------------------------------------+
3// | PHP Version 4                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 1997-2004 The PHP Group                                |
6// +----------------------------------------------------------------------+
7// | This source file is subject to version 3.0 of the PHP license,       |
8// | that is bundled with this package in the file LICENSE, and is        |
9// | available at through the world-wide-web at                           |
10// | http://www.php.net/license/3_0.txt.                                  |
11// | If you did not receive a copy of the PHP license and are unable to   |
12// | obtain it through the world-wide-web, please send a note to          |
13// | license@php.net so we can mail you a copy immediately.               |
14// +----------------------------------------------------------------------+
15// | Authors: Stephan Schmidt <schst@php.net>                             |
16// |          Aidan Lister <aidan@php.net>                                |
17// +----------------------------------------------------------------------+
18//
19// $Id$
20
21
22/**
23 * Replace function http_build_query()
24 *
25 * @category    PHP
26 * @package     PHP_Compat
27 * @link        http://php.net/function.http-build-query
28 * @author      Stephan Schmidt <schst@php.net>
29 * @author      Aidan Lister <aidan@php.net>
30 * @version     $Revision: 1.16 $
31 * @since       PHP 5
32 * @require     PHP 4.0.0 (user_error)
33 */
34if (!function_exists('http_build_query')) {
35    function http_build_query($formdata, $numeric_prefix = null)
36    {
37        // If $formdata is an object, convert it to an array
38        if (is_object($formdata)) {
39            $formdata = get_object_vars($formdata);
40        }
41
42        // Check we have an array to work with
43        if (!is_array($formdata)) {
44            user_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.',
45                E_USER_WARNING);
46            return false;
47        }
48
49        // If the array is empty, return null
50        if (empty($formdata)) {
51            return;
52        }
53
54        // Argument seperator
55        $separator = ini_get('arg_separator.output');
56
57        // Start building the query
58        $tmp = array ();
59        foreach ($formdata as $key => $val) {
60            if (is_integer($key) && $numeric_prefix != null) {
61                $key = $numeric_prefix . $key;
62            }
63
64            if (is_scalar($val)) {
65                array_push($tmp, urlencode($key).'='.urlencode($val));
66                continue;
67            }
68
69            // If the value is an array, recursively parse it
70            if (is_array($val)) {
71                array_push($tmp, __http_build_query($val, urlencode($key)));
72                continue;
73            }
74        }
75
76        return implode($separator, $tmp);
77    }
78
79    // Helper function
80    function __http_build_query ($array, $name)
81    {
82        $tmp = array ();
83        foreach ($array as $key => $value) {
84            if (is_array($value)) {
85                array_push($tmp, __http_build_query($value, sprintf('%s[%s]', $name, $key)));
86            } elseif (is_scalar($value)) {
87                array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value)));
88            } elseif (is_object($value)) {
89                array_push($tmp, __http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key)));
90            }
91        }
92
93        // Argument seperator
94        $separator = ini_get('arg_separator.output');
95
96        return implode($separator, $tmp);
97    }
98}
99
100?>
Note: See TracBrowser for help on using the repository browser.