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: http_build_query.php,v 1.16 2005/05/31 08:54:57 aidan Exp $ |
---|
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 | */ |
---|
34 | if (!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 | ?> |
---|