PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/lib/utf8/mbstring/core.php

https://bitbucket.org/fsx/shinobu-02
PHP | 140 lines | 50 code | 12 blank | 78 comment | 7 complexity | 80eb4c2cebcc016bb3fe07a3e2b9c808 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * @version $Id: core.php,v 1.5 2006/02/28 22:12:25 harryf Exp $
  4. * @package utf8
  5. * @subpackage strings
  6. */
  7. /**
  8. * Define UTF8_CORE as required
  9. */
  10. if (!defined('UTF8_CORE'))
  11. define('UTF8_CORE', true);
  12. /**
  13. * Wrapper round mb_strlen
  14. * Assumes you have mb_internal_encoding to UTF-8 already
  15. * Note: this function does not count bad bytes in the string - these
  16. * are simply ignored
  17. * @param string UTF-8 string
  18. * @return int number of UTF-8 characters in string
  19. * @package utf8
  20. * @subpackage strings
  21. */
  22. function utf8_strlen($str)
  23. {
  24. return mb_strlen($str);
  25. }
  26. /**
  27. * Assumes mbstring internal encoding is set to UTF-8
  28. * Wrapper around mb_strpos
  29. * Find position of first occurrence of a string
  30. * @param string haystack
  31. * @param string needle (you should validate this with utf8_is_valid)
  32. * @param integer offset in characters (from left)
  33. * @return mixed integer position or FALSE on failure
  34. * @package utf8
  35. * @subpackage strings
  36. */
  37. function utf8_strpos($str, $search, $offset=false)
  38. {
  39. if ($offset === false)
  40. return mb_strpos($str, $search);
  41. else
  42. return mb_strpos($str, $search, $offset);
  43. }
  44. /**
  45. * Assumes mbstring internal encoding is set to UTF-8
  46. * Wrapper around mb_strrpos
  47. * Find position of last occurrence of a char in a string
  48. * @param string haystack
  49. * @param string needle (you should validate this with utf8_is_valid)
  50. * @param integer (optional) offset (from left)
  51. * @return mixed integer position or FALSE on failure
  52. * @package utf8
  53. * @subpackage strings
  54. */
  55. function utf8_strrpos($str, $search, $offset=false)
  56. {
  57. if ($offset === false)
  58. {
  59. // Emulate behaviour of strrpos rather than raising warning
  60. if (empty($str))
  61. return false;
  62. return mb_strrpos($str, $search);
  63. }
  64. else
  65. {
  66. if (!is_int($offset))
  67. {
  68. trigger_error('utf8_strrpos expects parameter 3 to be long', E_USER_WARNING);
  69. return false;
  70. }
  71. $str = mb_substr($str, $offset);
  72. if (false !== ($pos = mb_strrpos($str, $search)))
  73. return $pos + $offset;
  74. return false;
  75. }
  76. }
  77. /**
  78. * Assumes mbstring internal encoding is set to UTF-8
  79. * Wrapper around mb_substr
  80. * Return part of a string given character offset (and optionally length)
  81. * @param string
  82. * @param integer number of UTF-8 characters offset (from left)
  83. * @param integer (optional) length in UTF-8 characters from offset
  84. * @return mixed string or FALSE if failure
  85. * @package utf8
  86. * @subpackage strings
  87. */
  88. function utf8_substr($str, $offset, $length=false)
  89. {
  90. if ($length === false)
  91. return mb_substr($str, $offset);
  92. else
  93. return mb_substr($str, $offset, $length);
  94. }
  95. /**
  96. * Assumes mbstring internal encoding is set to UTF-8
  97. * Wrapper around mb_strtolower
  98. * Make a string lowercase
  99. * Note: The concept of a characters "case" only exists is some alphabets
  100. * such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does
  101. * not exist in the Chinese alphabet, for example. See Unicode Standard
  102. * Annex #21: Case Mappings
  103. * @param string
  104. * @return mixed either string in lowercase or FALSE is UTF-8 invalid
  105. * @package utf8
  106. * @subpackage strings
  107. */
  108. function utf8_strtolower($str)
  109. {
  110. return mb_strtolower($str);
  111. }
  112. /**
  113. * Assumes mbstring internal encoding is set to UTF-8
  114. * Wrapper around mb_strtoupper
  115. * Make a string uppercase
  116. * Note: The concept of a characters "case" only exists is some alphabets
  117. * such as Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does
  118. * not exist in the Chinese alphabet, for example. See Unicode Standard
  119. * Annex #21: Case Mappings
  120. * @param string
  121. * @return mixed either string in lowercase or FALSE is UTF-8 invalid
  122. * @package utf8
  123. * @subpackage strings
  124. */
  125. function utf8_strtoupper($str)
  126. {
  127. return mb_strtoupper($str);
  128. }