stringNative.lib.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /** String Functions for phpMyAdmin
  4. * If mb_* functions don't exist, we create the ones we need and they'll use the
  5. * standard string functions.
  6. * All mb_* functions created by pMA should behave as mb_* functions.
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. if (!defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. //Define mb_* functions if they don't exist.
  14. if (!@function_exists('mb_strlen')) {
  15. /**
  16. * Returns length of string depending on current charset.
  17. *
  18. * @param string $string string to count
  19. *
  20. * @return int string length
  21. */
  22. function mb_strlen($string)
  23. {
  24. return strlen($string);
  25. }
  26. /**
  27. * Returns substring from string, works depending on current charset.
  28. *
  29. * @param string $string string to count
  30. * @param int $start start of substring
  31. * @param int $length length of substring
  32. *
  33. * @return string the sub string
  34. */
  35. function mb_substr($string, $start, $length = 2147483647)
  36. {
  37. if (null === $string || strlen($string) <= $start) {
  38. return '';
  39. }
  40. if (null === $length) {
  41. $length = 2147483647;
  42. }
  43. return substr($string, $start, $length);
  44. }
  45. /**
  46. * Returns number of substring from string.
  47. *
  48. * @param string $string string to check
  49. * @param int $needle string to count
  50. *
  51. * @return int number of substring from the string
  52. */
  53. function mb_substrCount($string, $needle)
  54. {
  55. return substr_count($string, $needle);
  56. }
  57. /**
  58. * Returns position of $needle in $haystack or false if not found
  59. *
  60. * @param string $haystack the string being checked
  61. * @param string $needle the string to find in haystack
  62. * @param int $offset the search offset
  63. *
  64. * @return integer position of $needle in $haystack or false
  65. */
  66. function mb_strpos($haystack, $needle, $offset = 0)
  67. {
  68. return strpos($haystack, $needle, $offset);
  69. }
  70. /**
  71. * Returns position of $needle in $haystack - case insensitive - or false if
  72. * not found
  73. *
  74. * @param string $haystack the string being checked
  75. * @param string $needle the string to find in haystack
  76. * @param int $offset the search offset
  77. *
  78. * @return integer position of $needle in $haystack or false
  79. */
  80. function mb_stripos($haystack, $needle, $offset = 0)
  81. {
  82. if (('' === $haystack || false === $haystack) && $offset >= strlen($haystack)
  83. ) {
  84. return false;
  85. }
  86. return stripos($haystack, $needle, $offset);
  87. }
  88. /**
  89. * Returns position of last $needle in $haystack or false if not found
  90. *
  91. * @param string $haystack the string being checked
  92. * @param string $needle the string to find in haystack
  93. * @param int $offset the search offset
  94. *
  95. * @return integer position of last $needle in $haystack or false
  96. */
  97. function mb_strrpos($haystack, $needle, $offset = 0)
  98. {
  99. return strrpos($haystack, $needle, $offset);
  100. }
  101. /**
  102. * Returns position of last $needle in $haystack - case insensitive - or false
  103. * if not found
  104. *
  105. * @param string $haystack the string being checked
  106. * @param string $needle the string to find in haystack
  107. * @param int $offset the search offset
  108. *
  109. * @return integer position of last $needle in $haystack or false
  110. */
  111. function mb_strripos($haystack, $needle, $offset = 0)
  112. {
  113. if (('' === $haystack || false === $haystack) && $offset >= strlen($haystack)
  114. ) {
  115. return false;
  116. }
  117. return strripos($haystack, $needle, $offset);
  118. }
  119. /**
  120. * Returns part of $haystack string starting from and including the first
  121. * occurrence of $needle to the end of $haystack or false if not found
  122. *
  123. * @param string $haystack the string being checked
  124. * @param string $needle the string to find in haystack
  125. * @param bool $before_needle the part before the needle
  126. *
  127. * @return string part of $haystack or false
  128. */
  129. function mb_strstr($haystack, $needle, $before_needle = false)
  130. {
  131. return strstr($haystack, $needle, $before_needle);
  132. }
  133. /**
  134. * Returns part of $haystack string starting from and including the first
  135. * occurrence of $needle to the end of $haystack - case insensitive - or false
  136. * if not found
  137. *
  138. * @param string $haystack the string being checked
  139. * @param string $needle the string to find in haystack
  140. * @param bool $before_needle the part before the needle
  141. *
  142. * @return string part of $haystack or false
  143. */
  144. function mb_stristr($haystack, $needle, $before_needle = false)
  145. {
  146. return stristr($haystack, $needle, $before_needle);
  147. }
  148. /**
  149. * Returns the portion of haystack which starts at the last occurrence or false
  150. * if not found
  151. *
  152. * @param string $haystack the string being checked
  153. * @param string $needle the string to find in haystack
  154. *
  155. * @return string portion of haystack which starts at the last occurrence or
  156. * false
  157. */
  158. function mb_strrchr($haystack, $needle)
  159. {
  160. return strrchr($haystack, $needle);
  161. }
  162. /**
  163. * Make a string lowercase
  164. *
  165. * @param string $string the string being lowercased
  166. *
  167. * @return string the lower case string
  168. */
  169. function mb_strtolower($string)
  170. {
  171. return strtolower($string);
  172. }
  173. /**
  174. * Make a string uppercase
  175. *
  176. * @param string $string the string being uppercased
  177. *
  178. * @return string the upper case string
  179. */
  180. function mb_strtoupper($string)
  181. {
  182. return strtoupper($string);
  183. }
  184. }
  185. //New functions.
  186. if (!@function_exists('mb_ord')) {
  187. /**
  188. * Perform a regular expression match
  189. *
  190. * @param string $pattern Pattern to search for
  191. * @param string $subject Input string
  192. * @param int $offset Start from search
  193. *
  194. * @return int 1 if matched, 0 if doesn't, false on failure
  195. */
  196. function mb_preg_strpos($pattern, $subject, $offset = 0)
  197. {
  198. $matches = array();
  199. $bFind = preg_match(
  200. $pattern, $subject, $matches, PREG_OFFSET_CAPTURE, $offset
  201. );
  202. if (1 !== $bFind) {
  203. return false;
  204. }
  205. return $matches[1][1];
  206. }
  207. /**
  208. * Get the ordinal value of a string
  209. *
  210. * @param string $string the string for which ord is required
  211. *
  212. * @return int the ord value
  213. */
  214. function mb_ord($string)
  215. {
  216. return ord($string);
  217. }
  218. /**
  219. * Get the character of an ASCII
  220. *
  221. * @param int $ascii the ASCII code for which character is required
  222. *
  223. * @return string the character
  224. */
  225. function mb_chr($ascii)
  226. {
  227. return chr($ascii);
  228. }
  229. }