PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/src/phpMyFAQ/Strings/Mbstring.php

http://github.com/thorsten/phpMyFAQ
PHP | 263 lines | 89 code | 23 blank | 151 comment | 6 complexity | fd0bf66ff8174af7edf410a3d7756519 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. // phpcs:ignoreFile
  3. /**
  4. * The string wrapper class using mbstring extension.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public License,
  7. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. * obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * @package phpMyFAQ
  11. * @author Anatoliy Belsky <ab@php.net>
  12. * @copyright 2009-2021 phpMyFAQ Team
  13. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  14. * @link https://www.phpmyfaq.de
  15. * @since 2009-04-06
  16. */
  17. namespace phpMyFAQ\Strings;
  18. use phpMyFAQ\Language;
  19. use phpMyFAQ\Strings\StringsAbstract;
  20. /**
  21. * Class Mbstring
  22. *
  23. * @package phpMyFAQ\Strings
  24. */
  25. class Mbstring extends StringsAbstract
  26. {
  27. /**
  28. * Instance.
  29. *
  30. * @var Mbstring
  31. */
  32. private static $instance;
  33. /**
  34. * Constructor.
  35. */
  36. final private function __construct()
  37. {
  38. }
  39. /**
  40. * Create and return an instance.
  41. *
  42. * @param string $language
  43. * @return Mbstring
  44. */
  45. public static function getInstance($language = 'en'): Mbstring
  46. {
  47. if (!(self::$instance instanceof Mbstring)) {
  48. self::$instance = new self();
  49. self::$instance->encoding = self::DEFAULT_ENCODING;
  50. self::$instance->language = Language::isASupportedLanguage($language) ? $language : self::DEFAULT_LANGUAGE;
  51. mb_regex_encoding(self::$instance->encoding);
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * Get string character count.
  57. *
  58. * @param string $str String
  59. * @return int
  60. */
  61. public function strlen(string $str): int
  62. {
  63. return mb_strlen($str, $this->encoding);
  64. }
  65. /**
  66. * Get a part of string.
  67. *
  68. * @param string $str String
  69. * @param int $start Start
  70. * @param null $length Length
  71. *
  72. * @return string
  73. */
  74. public function substr(string $str, int $start, $length = null): string
  75. {
  76. $length = null == $length ? mb_strlen($str) : $length;
  77. return mb_substr($str, $start, $length, $this->encoding);
  78. }
  79. /**
  80. * Get position of the first occurrence of a string.
  81. *
  82. * @param string $haystack Haystack
  83. * @param string $needle Needle
  84. * @param int $offset Offset
  85. *
  86. * @return int
  87. */
  88. public function strpos(string $haystack, string $needle, $offset = 0): int
  89. {
  90. return mb_strpos($haystack, $needle, $offset, $this->encoding);
  91. }
  92. /**
  93. * Make a string lower case.
  94. *
  95. * @param string $str String
  96. *
  97. * @return string
  98. */
  99. public function strtolower(string $str): string
  100. {
  101. return mb_strtolower($str, $this->encoding);
  102. }
  103. /**
  104. * Make a string upper case.
  105. *
  106. * @param string $str String
  107. *
  108. * @return string
  109. */
  110. public function strtoupper(string $str): string
  111. {
  112. return mb_strtoupper($str, $this->encoding);
  113. }
  114. /**
  115. * Get first occurrence of a string within another.
  116. *
  117. * @param string $haystack Haystack
  118. * @param string $needle Needle
  119. * @param bool $part Part
  120. *
  121. * @return string|false
  122. */
  123. public function strstr(string $haystack, string $needle, $part = false)
  124. {
  125. return mb_strstr($haystack, $needle, $part, $this->encoding);
  126. }
  127. /**
  128. * Count substring occurrences.
  129. *
  130. * @param string $haystack
  131. * @param string $needle
  132. *
  133. * @return int
  134. */
  135. public function substr_count(string $haystack, string $needle): int
  136. {
  137. return mb_substr_count($haystack, $needle, $this->encoding);
  138. }
  139. /**
  140. * Match a regexp.
  141. *
  142. * @param string $pattern
  143. * @param string $subject
  144. * @param null $matches
  145. * @param int $flags
  146. * @param int $offset
  147. *
  148. * @return int
  149. */
  150. public function preg_match(string $pattern, string $subject, &$matches = null, $flags = 0, $offset = 0): int
  151. {
  152. return preg_match(self::appendU($pattern), $subject, $matches, $flags, $offset);
  153. }
  154. /**
  155. * Match a regexp globally.
  156. *
  157. * @param string $pattern
  158. * @param string $subject
  159. * @param string[][] $matches
  160. * @param int $flags
  161. * @param int $offset
  162. *
  163. * @return int
  164. */
  165. public function preg_match_all(string $pattern, string $subject, array &$matches, $flags = 0, $offset = 0): int
  166. {
  167. return preg_match_all(self::appendU($pattern), $subject, $matches, $flags, $offset);
  168. }
  169. /**
  170. * Split string by a regexp.
  171. *
  172. * @param string $pattern
  173. * @param string $subject
  174. * @param int $limit
  175. * @param int $flags
  176. *
  177. * @return string[]|array|false
  178. */
  179. public function preg_split(string $pattern, string $subject, $limit = -1, $flags = 0)
  180. {
  181. return preg_split(self::appendU($pattern), $subject, $limit, $flags);
  182. }
  183. /**
  184. * Search and replace by a regexp using a callback.
  185. *
  186. * @param string|string[] $pattern
  187. * @param callable $callback
  188. * @param string|string[] $subject
  189. * @param int $limit
  190. * @param int $count
  191. *
  192. * @return string|string[]
  193. */
  194. public function preg_replace_callback($pattern, callable $callback, $subject, int $limit = -1, int &$count = 0)
  195. {
  196. if (is_array($pattern)) {
  197. foreach ($pattern as &$p) {
  198. $p = self::appendU($p);
  199. }
  200. } else {
  201. $pattern = self::appendU($pattern);
  202. }
  203. return preg_replace_callback($pattern, $callback, $subject, $limit, $count);
  204. }
  205. /**
  206. * Search and replace by a regexp.
  207. *
  208. * @param string|string[] $pattern
  209. * @param string|string[] $replacement
  210. * @param string|string[] $subject
  211. * @param int $limit
  212. * @param int $count
  213. *
  214. * @return string|string[]|null
  215. */
  216. public function preg_replace($pattern, $replacement, $subject, int $limit = -1, int &$count = 0)
  217. {
  218. if (is_array($pattern)) {
  219. foreach ($pattern as &$p) {
  220. $p = self::appendU($p);
  221. }
  222. } else {
  223. $pattern = self::appendU($pattern);
  224. }
  225. return preg_replace($pattern, $replacement, $subject, $limit, $count);
  226. }
  227. /**
  228. * Append an u to the string. The string is supposed
  229. * to be a regex prepared to use with a preg_* function.
  230. *
  231. * @param string $str
  232. *
  233. * @return string
  234. */
  235. private static function appendU(string $str): string
  236. {
  237. $str = (string)$str;
  238. return parent::isUTF8($str) ? $str . 'u' : $str;
  239. }
  240. }