PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/include/PHP/Compat/Function/http_build_query.php

https://github.com/usagi-project/mynets1
PHP | 100 lines | 49 code | 13 blank | 38 comment | 10 complexity | 0903b48b7f1259b7bbd89cc10e57e0f5 MD5 | raw file
  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. * Replace function http_build_query()
  22. *
  23. * @category PHP
  24. * @package PHP_Compat
  25. * @link http://php.net/function.http-build-query
  26. * @author Stephan Schmidt <schst@php.net>
  27. * @author Aidan Lister <aidan@php.net>
  28. * @version $Revision: 1.16 $
  29. * @since PHP 5
  30. * @require PHP 4.0.0 (user_error)
  31. */
  32. if (!function_exists('http_build_query')) {
  33. function http_build_query($formdata, $numeric_prefix = null)
  34. {
  35. // If $formdata is an object, convert it to an array
  36. if (is_object($formdata)) {
  37. $formdata = get_object_vars($formdata);
  38. }
  39. // Check we have an array to work with
  40. if (!is_array($formdata)) {
  41. user_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.',
  42. E_USER_WARNING);
  43. return false;
  44. }
  45. // If the array is empty, return null
  46. if (empty($formdata)) {
  47. return;
  48. }
  49. // Argument seperator
  50. $separator = ini_get('arg_separator.output');
  51. // Start building the query
  52. $tmp = array ();
  53. foreach ($formdata as $key => $val) {
  54. if (is_integer($key) && $numeric_prefix != null) {
  55. $key = $numeric_prefix . $key;
  56. }
  57. if (is_scalar($val)) {
  58. array_push($tmp, urlencode($key).'='.urlencode($val));
  59. continue;
  60. }
  61. // If the value is an array, recursively parse it
  62. if (is_array($val)) {
  63. array_push($tmp, __http_build_query($val, urlencode($key)));
  64. continue;
  65. }
  66. }
  67. return implode($separator, $tmp);
  68. }
  69. // Helper function
  70. function __http_build_query ($array, $name)
  71. {
  72. $tmp = array ();
  73. foreach ($array as $key => $value) {
  74. if (is_array($value)) {
  75. array_push($tmp, __http_build_query($value, sprintf('%s[%s]', $name, $key)));
  76. } elseif (is_scalar($value)) {
  77. array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value)));
  78. } elseif (is_object($value)) {
  79. array_push($tmp, __http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key)));
  80. }
  81. }
  82. // Argument seperator
  83. $separator = ini_get('arg_separator.output');
  84. return implode($separator, $tmp);
  85. }
  86. }
  87. ?>