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

/phpmyfaq/src/phpMyFAQ/Strings.php

http://github.com/thorsten/phpMyFAQ
PHP | 320 lines | 125 code | 23 blank | 172 comment | 4 complexity | 96b68bcb56fc532358b031477ac31356 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 main string wrapper class.
  5. *
  6. * The class uses mbstring extension if available. It's strongly recommended
  7. * to use and extend this class instead of using direct string functions. Doing so
  8. * you guarantees your code is upwards compatible with UTF-8 improvements. All
  9. * the string methods behaviour is identical to that of the same named
  10. * single byte string functions.
  11. * This Source Code Form is subject to the terms of the Mozilla Public License,
  12. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  13. * obtain one at http://mozilla.org/MPL/2.0/.
  14. *
  15. * @package phpMyFAQ
  16. * @author Anatoliy Belsky <ab@php.net>
  17. * @copyright 2009-2021 phpMyFAQ Team
  18. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  19. * @link https://www.phpmyfaq.de
  20. * @since 2009-04-06
  21. */
  22. namespace phpMyFAQ;
  23. use phpMyFAQ\Strings\Mbstring;
  24. use phpMyFAQ\Strings\StringBasic;
  25. /**
  26. * Class Strings
  27. *
  28. * @package phpMyFAQ
  29. */
  30. class Strings
  31. {
  32. /**
  33. * Instance.
  34. *
  35. * @var Mbstring|StringBasic|null
  36. */
  37. private static $instance = null;
  38. /**
  39. * Constructor.
  40. */
  41. final private function __construct()
  42. {
  43. }
  44. /**
  45. * Init.
  46. *
  47. * @param string $language Language
  48. */
  49. public static function init(string $language = 'en'): void
  50. {
  51. if (!self::$instance) {
  52. if (extension_loaded('mbstring') && function_exists('mb_regex_encoding')) {
  53. self::$instance = Mbstring::getInstance($language);
  54. } else {
  55. self::$instance = StringBasic::getInstance($language);
  56. }
  57. }
  58. }
  59. /**
  60. * Get current encoding.
  61. *
  62. * @return string
  63. */
  64. public static function getEncoding(): string
  65. {
  66. return self::$instance->getEncoding();
  67. }
  68. /**
  69. * Get string character count.
  70. *
  71. * @param string $str String
  72. * @return int
  73. */
  74. public static function strlen(string $str): int
  75. {
  76. return self::$instance->strlen($str);
  77. }
  78. /**
  79. * Get a part of string.
  80. *
  81. * @param string $string String
  82. * @param int $start Start
  83. * @param int|null $length Length
  84. * @return string
  85. */
  86. public static function substr(string $string, int $start, $length = 0): string
  87. {
  88. return self::$instance->substr($string, $start, $length);
  89. }
  90. /**
  91. * Get position of the first occurrence of a string.
  92. *
  93. * @param string $haystack Haystack
  94. * @param string $needle Needle
  95. * @param int $offset Offset
  96. * @return int
  97. */
  98. public static function strpos(string $haystack, string $needle, $offset = 0): int
  99. {
  100. return self::$instance->strpos($haystack, $needle, $offset);
  101. }
  102. /**
  103. * Make a string lower case.
  104. *
  105. * @param string $str String
  106. * @return string
  107. */
  108. public static function strtolower(string $str): string
  109. {
  110. return self::$instance->strtolower($str);
  111. }
  112. /**
  113. * Make a string upper case.
  114. *
  115. * @param string $str String
  116. * @return string
  117. */
  118. public static function strtoupper(string $str): string
  119. {
  120. return self::$instance->strtoupper($str);
  121. }
  122. /**
  123. * Get occurrence of a string within another.
  124. *
  125. * @param string $haystack Haystack
  126. * @param string $needle Needle
  127. * @param bool $part Part
  128. * @return string|false
  129. */
  130. public static function strstr(string $haystack, string $needle, $part = false)
  131. {
  132. return self::$instance->strstr($haystack, $needle, $part);
  133. }
  134. /**
  135. * Set current encoding.
  136. *
  137. * @param string $encoding
  138. */
  139. public static function setEncoding(string $encoding): void
  140. {
  141. self::$instance->setEncoding($encoding);
  142. }
  143. /**
  144. * Count substring occurrences.
  145. *
  146. * @param string $haystack
  147. * @param string $needle
  148. * @return int
  149. */
  150. public static function substr_count(string $haystack, string $needle): int // phpcs:ignore
  151. {
  152. return self::$instance->substr_count($haystack, $needle);
  153. }
  154. /**
  155. * Find position of last occurrence of a char in a string.
  156. *
  157. * @param string $haystack
  158. * @param string $needle
  159. * @param int $offset
  160. * @return int
  161. */
  162. public static function strrpos(string $haystack, string $needle, $offset = 0): int
  163. {
  164. return self::$instance->strrpos($haystack, $needle, $offset);
  165. }
  166. /**
  167. * Match a regexp.
  168. *
  169. * @param string $pattern
  170. * @param string $subject
  171. * @param null $matches
  172. * @param int $flags
  173. * @param int $offset
  174. * @return int
  175. */
  176. public static function preg_match(
  177. string $pattern,
  178. string $subject,
  179. &$matches = null,
  180. $flags = 0,
  181. $offset = 0
  182. ): int // phpcs:ignore
  183. {
  184. return self::$instance->preg_match($pattern, $subject, $matches, $flags, $offset);
  185. }
  186. /**
  187. * Match a regexp globally.
  188. *
  189. * @param string $pattern
  190. * @param string $subject
  191. * @param null $matches
  192. * @param int $flags
  193. * @param int $offset
  194. * @return int
  195. */
  196. public static function preg_match_all(
  197. string $pattern,
  198. string $subject,
  199. &$matches = null,
  200. $flags = 0,
  201. $offset = 0
  202. ): int // phpcs:ignore
  203. {
  204. return self::$instance->preg_match_all($pattern, $subject, $matches, $flags, $offset);
  205. }
  206. /**
  207. * Split string by a regexp.
  208. *
  209. * @param string $pattern
  210. * @param string $subject
  211. * @param int $limit
  212. * @param int $flags
  213. * @return string[]|array|false
  214. */
  215. public static function preg_split(string $pattern, string $subject, $limit = -1, $flags = 0) // phpcs:ignore
  216. {
  217. return self::$instance->preg_split($pattern, $subject, $limit, $flags);
  218. }
  219. /**
  220. * Search and replace by a regexp using a callback.
  221. *
  222. * @param string $pattern
  223. * @param callable $callback
  224. * @param string|string[] $subject
  225. * @param int $limit
  226. * @param int $count
  227. * @return string|string[]
  228. */
  229. public static function preg_replace_callback(
  230. string $pattern,
  231. callable $callback,
  232. $subject,
  233. $limit = -1,
  234. &$count = 0
  235. ) {
  236. return self::$instance->preg_replace_callback($pattern, $callback, $subject, $limit, $count);
  237. }
  238. /**
  239. * Search and replace by a regexp.
  240. *
  241. * @param string|string[] $pattern
  242. * @param string|string[] $replacement
  243. * @param string|string[] $subject
  244. * @param int $limit
  245. * @param int $count
  246. * @return string|string[]|null
  247. */
  248. public static function preg_replace($pattern, $replacement, $subject, $limit = -1, &$count = 0)
  249. {
  250. return self::$instance->preg_replace($pattern, $replacement, $subject, $limit, $count);
  251. }
  252. /**
  253. * Convert special chars to html entities.
  254. *
  255. * @param string|null $string The input string.
  256. * @param int $quoteStyle Quote style
  257. * @param string $charset Character set, UTF-8 by default
  258. * @param bool $doubleEncode If set to false, no encoding of existing entities
  259. * @return string
  260. */
  261. public static function htmlspecialchars(
  262. ?string $string,
  263. $quoteStyle = ENT_HTML5,
  264. $charset = 'utf-8',
  265. $doubleEncode = false
  266. ): string {
  267. return htmlspecialchars(
  268. $string,
  269. $quoteStyle,
  270. $charset,
  271. $doubleEncode
  272. );
  273. }
  274. /**
  275. * Convert all applicable characters to HTML entities.
  276. *
  277. * @param string $string The input string.
  278. * @param int $quoteStyle Quote style
  279. * @param string $charset Character set, UTF-8 by default
  280. * @param bool $doubleEncode If set to false, no encoding of existing entities
  281. * @return string
  282. */
  283. public static function htmlentities(
  284. string $string,
  285. $quoteStyle = ENT_HTML5,
  286. $charset = 'utf-8',
  287. $doubleEncode = true
  288. ): string
  289. {
  290. return htmlentities(
  291. $string,
  292. $quoteStyle,
  293. $charset,
  294. $doubleEncode
  295. );
  296. }
  297. }