mb_wrapper.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Elgg UTF-8 string functions
  4. *
  5. * @package Elgg
  6. * @subpackage Core
  7. */
  8. /**
  9. * Parses a string using mb_parse_str() if available.
  10. * NOTE: This differs from parse_str() by returning the results
  11. * instead of placing them in the local scope!
  12. *
  13. * @param string $str The string
  14. *
  15. * @return array
  16. * @since 1.7.0
  17. */
  18. function elgg_parse_str($str) {
  19. if (is_callable('mb_parse_str')) {
  20. mb_parse_str($str, $results);
  21. } else {
  22. parse_str($str, $results);
  23. }
  24. return $results;
  25. }
  26. /**
  27. * Wrapper function for mb_split(). Falls back to split() if
  28. * mb_split() isn't available. Parameters are passed to the
  29. * wrapped function in the same order they are passed to this
  30. * function.
  31. *
  32. * @return string
  33. * @since 1.7.0
  34. */
  35. function elgg_split() {
  36. $args = func_get_args();
  37. if (is_callable('mb_split')) {
  38. return call_user_func_array('mb_split', $args);
  39. }
  40. return call_user_func_array('split', $args);
  41. }
  42. /**
  43. * Wrapper function for mb_stristr(). Falls back to stristr() if
  44. * mb_stristr() isn't available. Parameters are passed to the
  45. * wrapped function in the same order they are passed to this
  46. * function.
  47. *
  48. * @return string
  49. * @since 1.7.0
  50. */
  51. function elgg_stristr() {
  52. $args = func_get_args();
  53. if (is_callable('mb_stristr')) {
  54. return call_user_func_array('mb_stristr', $args);
  55. }
  56. return call_user_func_array('stristr', $args);
  57. }
  58. /**
  59. * Wrapper function for mb_strlen(). Falls back to strlen() if
  60. * mb_strlen() isn't available. Parameters are passed to the
  61. * wrapped function in the same order they are passed to this
  62. * function.
  63. *
  64. * @return string
  65. * @since 1.7.0
  66. */
  67. function elgg_strlen() {
  68. $args = func_get_args();
  69. if (is_callable('mb_strlen')) {
  70. return call_user_func_array('mb_strlen', $args);
  71. }
  72. return call_user_func_array('strlen', $args);
  73. }
  74. /**
  75. * Wrapper function for mb_strpos(). Falls back to strpos() if
  76. * mb_strpos() isn't available. Parameters are passed to the
  77. * wrapped function in the same order they are passed to this
  78. * function.
  79. *
  80. * @return string
  81. * @since 1.7.0
  82. */
  83. function elgg_strpos() {
  84. $args = func_get_args();
  85. if (is_callable('mb_strpos')) {
  86. return call_user_func_array('mb_strpos', $args);
  87. }
  88. return call_user_func_array('strpos', $args);
  89. }
  90. /**
  91. * Wrapper function for mb_strrchr(). Falls back to strrchr() if
  92. * mb_strrchr() isn't available. Parameters are passed to the
  93. * wrapped function in the same order they are passed to this
  94. * function.
  95. *
  96. * @return string
  97. * @since 1.7.0
  98. */
  99. function elgg_strrchr() {
  100. $args = func_get_args();
  101. if (is_callable('mb_strrchr')) {
  102. return call_user_func_array('mb_strrchr', $args);
  103. }
  104. return call_user_func_array('strrchr', $args);
  105. }
  106. /**
  107. * Wrapper function for mb_strripos(). Falls back to strripos() if
  108. * mb_strripos() isn't available. Parameters are passed to the
  109. * wrapped function in the same order they are passed to this
  110. * function.
  111. *
  112. * @return int
  113. * @since 1.7.0
  114. */
  115. function elgg_strripos() {
  116. $args = func_get_args();
  117. if (is_callable('mb_strripos')) {
  118. return call_user_func_array('mb_strripos', $args);
  119. }
  120. return call_user_func_array('strripos', $args);
  121. }
  122. /**
  123. * Wrapper function for mb_strrpos(). Falls back to strrpos() if
  124. * mb_strrpos() isn't available. Parameters are passed to the
  125. * wrapped function in the same order they are passed to this
  126. * function.
  127. *
  128. * @return int
  129. * @since 1.7.0
  130. */
  131. function elgg_strrpos() {
  132. $args = func_get_args();
  133. if (is_callable('mb_strrpos')) {
  134. return call_user_func_array('mb_strrpos', $args);
  135. }
  136. return call_user_func_array('strrpos', $args);
  137. }
  138. /**
  139. * Wrapper function for mb_strstr(). Falls back to strstr() if
  140. * mb_strstr() isn't available. Parameters are passed to the
  141. * wrapped function in the same order they are passed to this
  142. * function.
  143. *
  144. * @return bool
  145. * @since 1.7.0
  146. */
  147. function elgg_strstr() {
  148. $args = func_get_args();
  149. if (is_callable('mb_strstr')) {
  150. return call_user_func_array('mb_strstr', $args);
  151. }
  152. return call_user_func_array('strstr', $args);
  153. }
  154. /**
  155. * Wrapper function for mb_strtolower(). Falls back to strtolower() if
  156. * mb_strtolower() isn't available. Parameters are passed to the
  157. * wrapped function in the same order they are passed to this
  158. * function.
  159. *
  160. * @return string
  161. * @since 1.7.0
  162. */
  163. function elgg_strtolower() {
  164. $args = func_get_args();
  165. if (is_callable('mb_strtolower')) {
  166. return call_user_func_array('mb_strtolower', $args);
  167. }
  168. return call_user_func_array('strtolower', $args);
  169. }
  170. /**
  171. * Wrapper function for mb_strtoupper(). Falls back to strtoupper() if
  172. * mb_strtoupper() isn't available. Parameters are passed to the
  173. * wrapped function in the same order they are passed to this
  174. * function.
  175. *
  176. * @return string
  177. * @since 1.7.0
  178. */
  179. function elgg_strtoupper() {
  180. $args = func_get_args();
  181. if (is_callable('mb_strtoupper')) {
  182. return call_user_func_array('mb_strtoupper', $args);
  183. }
  184. return call_user_func_array('strtoupper', $args);
  185. }
  186. /**
  187. * Wrapper function for mb_substr_count(). Falls back to substr_count() if
  188. * mb_substr_count() isn't available. Parameters are passed to the
  189. * wrapped function in the same order they are passed to this
  190. * function.
  191. *
  192. * @return int
  193. * @since 1.7.0
  194. */
  195. function elgg_substr_count() {
  196. $args = func_get_args();
  197. if (is_callable('mb_substr_count')) {
  198. return call_user_func_array('mb_substr_count', $args);
  199. }
  200. return call_user_func_array('substr_count', $args);
  201. }
  202. /**
  203. * Wrapper function for mb_substr(). Falls back to substr() if
  204. * mb_substr() isn't available. Parameters are passed to the
  205. * wrapped function in the same order they are passed to this
  206. * function.
  207. *
  208. * @return string
  209. * @since 1.7.0
  210. */
  211. function elgg_substr() {
  212. $args = func_get_args();
  213. if (is_callable('mb_substr')) {
  214. return call_user_func_array('mb_substr', $args);
  215. }
  216. return call_user_func_array('substr', $args);
  217. }
  218. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  219. // if mb functions are available, set internal encoding to UTF8
  220. if (is_callable('mb_internal_encoding')) {
  221. mb_internal_encoding("UTF-8");
  222. if (version_compare('5.6.0', PHP_VERSION, '<')) {
  223. if (ini_get("mbstring.internal_encoding")) {
  224. ini_set("mbstring.internal_encoding", 'UTF-8');
  225. }
  226. }
  227. }
  228. };