PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/functions/functions.mb.inc.php

https://github.com/xong/rexsearch
PHP | 121 lines | 107 code | 7 blank | 7 comment | 15 complexity | 9eba428a9e59e862c7ee18a77a418cc8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // This file defines mb-functions for backward compatibility
  3. if(!function_exists('mb_strlen'))
  4. {
  5. function mb_strlen($str, $encoding = 'UTF-8')
  6. {
  7. return strtolower($encoding) == 'utf-8' ? strlen(utf8_decode($str)) : strlen($str);
  8. }
  9. }
  10. if(!function_exists('mb_strtolower'))
  11. {
  12. function mb_strtolower($str, $encoding = 'UTF-8')
  13. {
  14. return strtolower($encoding) == 'utf-8' ? strtolower(utf8_decode($str)) : strtolower($str);
  15. }
  16. }
  17. if(!function_exists('mb_internal_encoding'))
  18. {
  19. function mb_internal_encoding()
  20. {
  21. return rex_lang_is_utf8() ? 'UTF-8' : 'ISO-8859-15';
  22. }
  23. }
  24. if(!function_exists('mb_detect_encoding'))
  25. {
  26. function mb_detect_encoding($str)
  27. {
  28. foreach (array('UTF-8', 'ISO-8859-15', 'WINDOWS-1251') as $encoding)
  29. {
  30. $sample = @iconv($encoding, $encoding, $str);
  31. if(md5($sample) == md5($str))
  32. return $encoding;
  33. }
  34. return '8bit';
  35. }
  36. }
  37. /*
  38. * Author: Steve
  39. * Source: http://www.php.net/manual/de/function.json-encode.php#82904
  40. * Date: 01-May-2008 12:35
  41. */
  42. if (!function_exists('json_encode'))
  43. {
  44. function json_encode($a=false)
  45. {
  46. if (is_null($a)) return 'null';
  47. if ($a === false) return 'false';
  48. if ($a === true) return 'true';
  49. if (is_scalar($a))
  50. {
  51. if (is_float($a))
  52. {
  53. // Always use "." for floats.
  54. return floatval(str_replace(",", ".", strval($a)));
  55. }
  56. if (is_string($a))
  57. {
  58. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  59. return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
  60. }
  61. else
  62. return $a;
  63. }
  64. $isList = true;
  65. for ($i = 0, reset($a); $i < count($a); $i++, next($a))
  66. {
  67. if (key($a) !== $i)
  68. {
  69. $isList = false;
  70. break;
  71. }
  72. }
  73. $result = array();
  74. if ($isList)
  75. {
  76. foreach ($a as $v) $result[] = json_encode($v);
  77. return '[' . join(',', $result) . ']';
  78. }
  79. else
  80. {
  81. foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
  82. return '{' . join(',', $result) . '}';
  83. }
  84. }
  85. }
  86. /*
  87. * Author: www at walidator dot info
  88. * Source: http://de.php.net/manual/de/function.json-decode.php#91216
  89. * Date: 30-May-2009 02:16
  90. */
  91. if ( !function_exists('json_decode') ){
  92. function json_decode($json)
  93. {
  94. // Author: walidator.info 2009
  95. $comment = false;
  96. $out = '$x=';
  97. for ($i=0; $i<strlen($json); $i++)
  98. {
  99. if (!$comment)
  100. {
  101. if ($json[$i] == '{') $out .= ' array(';
  102. else if ($json[$i] == '}') $out .= ')';
  103. else if ($json[$i] == ':') $out .= '=>';
  104. else $out .= $json[$i];
  105. }
  106. else $out .= $json[$i];
  107. if ($json[$i] == '"') $comment = !$comment;
  108. }
  109. eval($out . ';');
  110. return $x;
  111. }
  112. }