utf8.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. * UTF8 helper functions
  4. *
  5. * This file now only intitializes the UTF-8 capability detection and defines helper
  6. * functions if needed. All actual code is in the \dokuwiki\Utf8 classes
  7. *
  8. * @author Andreas Gohr <andi@splitbrain.org>
  9. */
  10. use dokuwiki\Utf8\Clean;
  11. use dokuwiki\Utf8\Conversion;
  12. use dokuwiki\Utf8\PhpString;
  13. use dokuwiki\Utf8\Unicode;
  14. /**
  15. * check for mb_string support
  16. */
  17. if (!defined('UTF8_MBSTRING')) {
  18. if (function_exists('mb_substr') && !defined('UTF8_NOMBSTRING')) {
  19. define('UTF8_MBSTRING', 1);
  20. } else {
  21. define('UTF8_MBSTRING', 0);
  22. }
  23. }
  24. /**
  25. * Check if PREG was compiled with UTF-8 support
  26. *
  27. * Without this many of the functions below will not work, so this is a minimal requirement
  28. */
  29. if (!defined('UTF8_PREGSUPPORT')) {
  30. define('UTF8_PREGSUPPORT', (bool)@preg_match('/^.$/u', 'ñ'));
  31. }
  32. /**
  33. * Check if PREG was compiled with Unicode Property support
  34. *
  35. * This is not required for the functions below, but might be needed in a UTF-8 aware application
  36. */
  37. if (!defined('UTF8_PROPERTYSUPPORT')) {
  38. define('UTF8_PROPERTYSUPPORT', (bool)@preg_match('/^\pL$/u', 'ñ'));
  39. }
  40. if (UTF8_MBSTRING) {
  41. mb_internal_encoding('UTF-8');
  42. }
  43. if (!function_exists('utf8_isASCII')) {
  44. /** @deprecated 2019-06-09 */
  45. function utf8_isASCII($str)
  46. {
  47. dbg_deprecated(Clean::class . '::isASCII()');
  48. return Clean::isASCII($str);
  49. }
  50. }
  51. if (!function_exists('utf8_strip')) {
  52. /** @deprecated 2019-06-09 */
  53. function utf8_strip($str)
  54. {
  55. dbg_deprecated(Clean::class . '::strip()');
  56. return Clean::strip($str);
  57. }
  58. }
  59. if (!function_exists('utf8_check')) {
  60. /** @deprecated 2019-06-09 */
  61. function utf8_check($str)
  62. {
  63. dbg_deprecated(Clean::class . '::isUtf8()');
  64. return Clean::isUtf8($str);
  65. }
  66. }
  67. if (!function_exists('utf8_basename')) {
  68. /** @deprecated 2019-06-09 */
  69. function utf8_basename($path, $suffix = '')
  70. {
  71. dbg_deprecated(PhpString::class . '::basename()');
  72. return PhpString::basename($path, $suffix);
  73. }
  74. }
  75. if (!function_exists('utf8_strlen')) {
  76. /** @deprecated 2019-06-09 */
  77. function utf8_strlen($str)
  78. {
  79. dbg_deprecated(PhpString::class . '::strlen()');
  80. return PhpString::strlen($str);
  81. }
  82. }
  83. if (!function_exists('utf8_substr')) {
  84. /** @deprecated 2019-06-09 */
  85. function utf8_substr($str, $offset, $length = null)
  86. {
  87. dbg_deprecated(PhpString::class . '::substr()');
  88. return PhpString::substr($str, $offset, $length);
  89. }
  90. }
  91. if (!function_exists('utf8_substr_replace')) {
  92. /** @deprecated 2019-06-09 */
  93. function utf8_substr_replace($string, $replacement, $start, $length = 0)
  94. {
  95. dbg_deprecated(PhpString::class . '::substr_replace()');
  96. return PhpString::substr_replace($string, $replacement, $start, $length);
  97. }
  98. }
  99. if (!function_exists('utf8_ltrim')) {
  100. /** @deprecated 2019-06-09 */
  101. function utf8_ltrim($str, $charlist = '')
  102. {
  103. dbg_deprecated(PhpString::class . '::ltrim()');
  104. return PhpString::ltrim($str, $charlist);
  105. }
  106. }
  107. if (!function_exists('utf8_rtrim')) {
  108. /** @deprecated 2019-06-09 */
  109. function utf8_rtrim($str, $charlist = '')
  110. {
  111. dbg_deprecated(PhpString::class . '::rtrim()');
  112. return PhpString::rtrim($str, $charlist);
  113. }
  114. }
  115. if (!function_exists('utf8_trim')) {
  116. /** @deprecated 2019-06-09 */
  117. function utf8_trim($str, $charlist = '')
  118. {
  119. dbg_deprecated(PhpString::class . '::trim()');
  120. return PhpString::trim($str, $charlist);
  121. }
  122. }
  123. if (!function_exists('utf8_strtolower')) {
  124. /** @deprecated 2019-06-09 */
  125. function utf8_strtolower($str)
  126. {
  127. dbg_deprecated(PhpString::class . '::strtolower()');
  128. return PhpString::strtolower($str);
  129. }
  130. }
  131. if (!function_exists('utf8_strtoupper')) {
  132. /** @deprecated 2019-06-09 */
  133. function utf8_strtoupper($str)
  134. {
  135. dbg_deprecated(PhpString::class . '::strtoupper()');
  136. return PhpString::strtoupper($str);
  137. }
  138. }
  139. if (!function_exists('utf8_ucfirst')) {
  140. /** @deprecated 2019-06-09 */
  141. function utf8_ucfirst($str)
  142. {
  143. dbg_deprecated(PhpString::class . '::ucfirst()');
  144. return PhpString::ucfirst($str);
  145. }
  146. }
  147. if (!function_exists('utf8_ucwords')) {
  148. /** @deprecated 2019-06-09 */
  149. function utf8_ucwords($str)
  150. {
  151. dbg_deprecated(PhpString::class . '::ucwords()');
  152. return PhpString::ucwords($str);
  153. }
  154. }
  155. if (!function_exists('utf8_deaccent')) {
  156. /** @deprecated 2019-06-09 */
  157. function utf8_deaccent($str, $case = 0)
  158. {
  159. dbg_deprecated(Clean::class . '::deaccent()');
  160. return Clean::deaccent($str, $case);
  161. }
  162. }
  163. if (!function_exists('utf8_romanize')) {
  164. /** @deprecated 2019-06-09 */
  165. function utf8_romanize($str)
  166. {
  167. dbg_deprecated(Clean::class . '::romanize()');
  168. return Clean::romanize($str);
  169. }
  170. }
  171. if (!function_exists('utf8_stripspecials')) {
  172. /** @deprecated 2019-06-09 */
  173. function utf8_stripspecials($str, $repl = '', $additional = '')
  174. {
  175. dbg_deprecated(Clean::class . '::stripspecials()');
  176. return Clean::stripspecials($str, $repl, $additional);
  177. }
  178. }
  179. if (!function_exists('utf8_strpos')) {
  180. /** @deprecated 2019-06-09 */
  181. function utf8_strpos($haystack, $needle, $offset = 0)
  182. {
  183. dbg_deprecated(PhpString::class . '::strpos()');
  184. return PhpString::strpos($haystack, $needle, $offset);
  185. }
  186. }
  187. if (!function_exists('utf8_tohtml')) {
  188. /** @deprecated 2019-06-09 */
  189. function utf8_tohtml($str, $all = false)
  190. {
  191. dbg_deprecated(Conversion::class . '::toHtml()');
  192. return Conversion::toHtml($str, $all);
  193. }
  194. }
  195. if (!function_exists('utf8_unhtml')) {
  196. /** @deprecated 2019-06-09 */
  197. function utf8_unhtml($str, $enties = false)
  198. {
  199. dbg_deprecated(Conversion::class . '::fromHtml()');
  200. return Conversion::fromHtml($str, $enties);
  201. }
  202. }
  203. if (!function_exists('utf8_to_unicode')) {
  204. /** @deprecated 2019-06-09 */
  205. function utf8_to_unicode($str, $strict = false)
  206. {
  207. dbg_deprecated(Unicode::class . '::fromUtf8()');
  208. return Unicode::fromUtf8($str, $strict);
  209. }
  210. }
  211. if (!function_exists('unicode_to_utf8')) {
  212. /** @deprecated 2019-06-09 */
  213. function unicode_to_utf8($arr, $strict = false)
  214. {
  215. dbg_deprecated(Unicode::class . '::toUtf8()');
  216. return Unicode::toUtf8($arr, $strict);
  217. }
  218. }
  219. if (!function_exists('utf8_to_utf16be')) {
  220. /** @deprecated 2019-06-09 */
  221. function utf8_to_utf16be($str, $bom = false)
  222. {
  223. dbg_deprecated(Conversion::class . '::toUtf16be()');
  224. return Conversion::toUtf16be($str, $bom);
  225. }
  226. }
  227. if (!function_exists('utf16be_to_utf8')) {
  228. /** @deprecated 2019-06-09 */
  229. function utf16be_to_utf8($str)
  230. {
  231. dbg_deprecated(Conversion::class . '::fromUtf16be()');
  232. return Conversion::fromUtf16be($str);
  233. }
  234. }
  235. if (!function_exists('utf8_bad_replace')) {
  236. /** @deprecated 2019-06-09 */
  237. function utf8_bad_replace($str, $replace = '')
  238. {
  239. dbg_deprecated(Clean::class . '::replaceBadBytes()');
  240. return Clean::replaceBadBytes($str, $replace);
  241. }
  242. }
  243. if (!function_exists('utf8_correctIdx')) {
  244. /** @deprecated 2019-06-09 */
  245. function utf8_correctIdx($str, $i, $next = false)
  246. {
  247. dbg_deprecated(Clean::class . '::correctIdx()');
  248. return Clean::correctIdx($str, $i, $next);
  249. }
  250. }