Str.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. namespace Yansongda\Supports;
  3. use Exception;
  4. /**
  5. * modify from Illuminate\Support.
  6. */
  7. class Str
  8. {
  9. /**
  10. * The cache of snake-cased words.
  11. *
  12. * @var array
  13. */
  14. protected static $snakeCache = [];
  15. /**
  16. * The cache of camel-cased words.
  17. *
  18. * @var array
  19. */
  20. protected static $camelCache = [];
  21. /**
  22. * The cache of studly-cased words.
  23. *
  24. * @var array
  25. */
  26. protected static $studlyCache = [];
  27. /**
  28. * Return the remainder of a string after a given value.
  29. */
  30. public static function after(string $subject, string $search): string
  31. {
  32. return '' === $search ? $subject : array_reverse(explode($search, $subject, 2))[0];
  33. }
  34. /**
  35. * Transliterate a UTF-8 value to ASCII.
  36. */
  37. public static function ascii(string $value, string $language = 'en'): string
  38. {
  39. $languageSpecific = static::languageSpecificCharsArray($language);
  40. if (!is_null($languageSpecific)) {
  41. $value = str_replace($languageSpecific[0], $languageSpecific[1], $value);
  42. }
  43. foreach (static::charsArray() as $key => $val) {
  44. $value = str_replace($val, $key, $value);
  45. }
  46. return preg_replace('/[^\x20-\x7E]/u', '', $value);
  47. }
  48. /**
  49. * Get the portion of a string before a given value.
  50. */
  51. public static function before(string $subject, string $search): string
  52. {
  53. return '' === $search ? $subject : explode($search, $subject)[0];
  54. }
  55. /**
  56. * Convert a value to camel case.
  57. */
  58. public static function camel(string $value): string
  59. {
  60. if (isset(static::$camelCache[$value])) {
  61. return static::$camelCache[$value];
  62. }
  63. return static::$camelCache[$value] = lcfirst(static::studly($value));
  64. }
  65. /**
  66. * Determine if a given string contains a given substring.
  67. *
  68. * @param string|array $needles
  69. */
  70. public static function contains(string $haystack, $needles): bool
  71. {
  72. foreach ((array) $needles as $needle) {
  73. if ('' !== $needle && false !== mb_strpos($haystack, $needle)) {
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. /**
  80. * Determine if a given string ends with a given substring.
  81. *
  82. * @param string|array $needles
  83. */
  84. public static function endsWith(string $haystack, $needles): bool
  85. {
  86. foreach ((array) $needles as $needle) {
  87. if (substr($haystack, -strlen($needle)) === (string) $needle) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. * Cap a string with a single instance of a given value.
  95. */
  96. public static function finish(string $value, string $cap): string
  97. {
  98. $quoted = preg_quote($cap, '/');
  99. return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
  100. }
  101. /**
  102. * Determine if a given string matches a given pattern.
  103. *
  104. * @param string|array $pattern
  105. */
  106. public static function is($pattern, string $value): bool
  107. {
  108. $patterns = is_array($pattern) ? $pattern : (array) $pattern;
  109. if (empty($patterns)) {
  110. return false;
  111. }
  112. foreach ($patterns as $pattern) {
  113. // If the given value is an exact match we can of course return true right
  114. // from the beginning. Otherwise, we will translate asterisks and do an
  115. // actual pattern match against the two strings to see if they match.
  116. if ($pattern == $value) {
  117. return true;
  118. }
  119. $pattern = preg_quote($pattern, '#');
  120. // Asterisks are translated into zero-or-more regular expression wildcards
  121. // to make it convenient to check if the strings starts with the given
  122. // pattern such as "library/*", making any string check convenient.
  123. $pattern = str_replace('\*', '.*', $pattern);
  124. if (1 === preg_match('#^'.$pattern.'\z#u', $value)) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. * Convert a string to kebab case.
  132. */
  133. public static function kebab(string $value): string
  134. {
  135. return static::snake($value, '-');
  136. }
  137. /**
  138. * Return the length of the given string.
  139. *
  140. * @param string $encoding
  141. */
  142. public static function length(string $value, ?string $encoding = null): int
  143. {
  144. if (null !== $encoding) {
  145. return mb_strlen($value, $encoding);
  146. }
  147. return mb_strlen($value);
  148. }
  149. /**
  150. * Limit the number of characters in a string.
  151. */
  152. public static function limit(string $value, int $limit = 100, string $end = '...'): string
  153. {
  154. if (mb_strwidth($value, 'UTF-8') <= $limit) {
  155. return $value;
  156. }
  157. return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
  158. }
  159. /**
  160. * Convert the given string to lower-case.
  161. */
  162. public static function lower(string $value): string
  163. {
  164. return mb_strtolower($value, 'UTF-8');
  165. }
  166. /**
  167. * Limit the number of words in a string.
  168. */
  169. public static function words(string $value, int $words = 100, string $end = '...'): string
  170. {
  171. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
  172. if (!isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  173. return $value;
  174. }
  175. return rtrim($matches[0]).$end;
  176. }
  177. /**
  178. * Parse a Class.
  179. */
  180. public static function parseCallback(string $callback, ?string $default = null): array
  181. {
  182. return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
  183. }
  184. /**
  185. * Generate a more truly "random" alpha-numeric string.
  186. *
  187. * @throws Exception
  188. */
  189. public static function random(int $length = 16): string
  190. {
  191. $string = '';
  192. while (($len = strlen($string)) < $length) {
  193. $size = $length - $len;
  194. $bytes = function_exists('random_bytes') ? random_bytes($size) : mt_rand();
  195. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  196. }
  197. return $string;
  198. }
  199. /**
  200. * Replace a given value in the string sequentially with an array.
  201. */
  202. public static function replaceArray(string $search, array $replace, string $subject): string
  203. {
  204. foreach ($replace as $value) {
  205. $subject = static::replaceFirst($search, $value, $subject);
  206. }
  207. return $subject;
  208. }
  209. /**
  210. * Replace the first occurrence of a given value in the string.
  211. */
  212. public static function replaceFirst(string $search, string $replace, string $subject): string
  213. {
  214. if ('' == $search) {
  215. return $subject;
  216. }
  217. $position = strpos($subject, $search);
  218. if (false !== $position) {
  219. return substr_replace($subject, $replace, $position, strlen($search));
  220. }
  221. return $subject;
  222. }
  223. /**
  224. * Replace the last occurrence of a given value in the string.
  225. */
  226. public static function replaceLast(string $search, string $replace, string $subject): string
  227. {
  228. $position = strrpos($subject, $search);
  229. if (false !== $position) {
  230. return substr_replace($subject, $replace, $position, strlen($search));
  231. }
  232. return $subject;
  233. }
  234. /**
  235. * Begin a string with a single instance of a given value.
  236. */
  237. public static function start(string $value, string $prefix): string
  238. {
  239. $quoted = preg_quote($prefix, '/');
  240. return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
  241. }
  242. /**
  243. * Convert the given string to upper-case.
  244. */
  245. public static function upper(string $value): string
  246. {
  247. return mb_strtoupper($value, 'UTF-8');
  248. }
  249. /**
  250. * Convert the given string to title case.
  251. */
  252. public static function title(string $value): string
  253. {
  254. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  255. }
  256. /**
  257. * Generate a URL friendly "slug" from a given string.
  258. */
  259. public static function slug(string $title, string $separator = '-', string $language = 'en'): string
  260. {
  261. $title = static::ascii($title, $language);
  262. // Convert all dashes/underscores into separator
  263. $flip = '-' == $separator ? '_' : '-';
  264. $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
  265. // Replace @ with the word 'at'
  266. $title = str_replace('@', $separator.'at'.$separator, $title);
  267. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  268. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
  269. // Replace all separator characters and whitespace by a single separator
  270. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  271. return trim($title, $separator);
  272. }
  273. /**
  274. * Convert a string to snake case.
  275. */
  276. public static function snake(string $value, string $delimiter = '_'): string
  277. {
  278. $key = $value;
  279. if (isset(static::$snakeCache[$key][$delimiter])) {
  280. return static::$snakeCache[$key][$delimiter];
  281. }
  282. if (!ctype_lower($value)) {
  283. $value = preg_replace('/\s+/u', '', ucwords($value));
  284. $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
  285. }
  286. return static::$snakeCache[$key][$delimiter] = $value;
  287. }
  288. /**
  289. * Determine if a given string starts with a given substring.
  290. *
  291. * @param string|array $needles
  292. */
  293. public static function startsWith(string $haystack, $needles): bool
  294. {
  295. foreach ((array) $needles as $needle) {
  296. if ('' !== $needle && substr($haystack, 0, strlen($needle)) === (string) $needle) {
  297. return true;
  298. }
  299. }
  300. return false;
  301. }
  302. /**
  303. * Convert a value to studly caps case.
  304. */
  305. public static function studly(string $value): string
  306. {
  307. $key = $value;
  308. if (isset(static::$studlyCache[$key])) {
  309. return static::$studlyCache[$key];
  310. }
  311. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  312. return static::$studlyCache[$key] = str_replace(' ', '', $value);
  313. }
  314. /**
  315. * Returns the portion of string specified by the start and length parameters.
  316. */
  317. public static function substr(string $string, int $start, ?int $length = null): string
  318. {
  319. return mb_substr($string, $start, $length, 'UTF-8');
  320. }
  321. /**
  322. * Make a string's first character uppercase.
  323. */
  324. public static function ucfirst(string $string): string
  325. {
  326. return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
  327. }
  328. /**
  329. * Convert string's encoding.
  330. *
  331. * @author yansongda <me@yansonga.cn>
  332. */
  333. public static function encoding(string $string, string $to = 'utf-8', string $from = 'gb2312'): string
  334. {
  335. return mb_convert_encoding($string, $to, $from);
  336. }
  337. /**
  338. * Returns the replacements for the ascii method.
  339. *
  340. * Note: Adapted from Stringy\Stringy.
  341. *
  342. * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
  343. */
  344. protected static function charsArray(): array
  345. {
  346. static $charsArray;
  347. if (isset($charsArray)) {
  348. return $charsArray;
  349. }
  350. return $charsArray = [
  351. '0' => ['°', '₀', '۰', '0'],
  352. '1' => ['¹', '₁', '۱', '1'],
  353. '2' => ['²', '₂', '۲', '2'],
  354. '3' => ['³', '₃', '۳', '3'],
  355. '4' => ['⁴', '₄', '۴', '٤', '4'],
  356. '5' => ['⁵', '₅', '۵', '٥', '5'],
  357. '6' => ['⁶', '₆', '۶', '٦', '6'],
  358. '7' => ['⁷', '₇', '۷', '7'],
  359. '8' => ['⁸', '₈', '۸', '8'],
  360. '9' => ['⁹', '₉', '۹', '9'],
  361. 'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ', 'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا', 'a', 'ä'],
  362. 'b' => ['б', 'β', 'ب', 'ဗ', 'ბ', 'b'],
  363. 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ', 'c'],
  364. 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', 'd'],
  365. 'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э', 'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ', 'e'],
  366. 'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ', 'f'],
  367. 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g'],
  368. 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', 'h'],
  369. 'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი', 'इ', 'ی', 'i'],
  370. 'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', 'j'],
  371. 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک', 'k'],
  372. 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l'],
  373. 'm' => ['м', 'μ', 'م', 'မ', 'მ', 'm'],
  374. 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ', 'n'],
  375. 'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ', 'o', 'ö'],
  376. 'p' => ['п', 'π', 'ပ', 'პ', 'پ', 'p'],
  377. 'q' => ['ყ', 'q'],
  378. 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ', 'r'],
  379. 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს', 's'],
  380. 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ', 't'],
  381. 'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у', 'ဉ', 'ု', 'ူ', 'ǔ', 'ǖ', 'ǘ', 'ǚ', 'ǜ', 'უ', 'उ', 'u', 'ў', 'ü'],
  382. 'v' => ['в', 'ვ', 'ϐ', 'v'],
  383. 'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ', 'w'],
  384. 'x' => ['χ', 'ξ', 'x'],
  385. 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ', 'y'],
  386. 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', 'z'],
  387. 'aa' => ['ع', 'आ', 'آ'],
  388. 'ae' => ['æ', 'ǽ'],
  389. 'ai' => ['ऐ'],
  390. 'ch' => ['ч', 'ჩ', 'ჭ', 'چ'],
  391. 'dj' => ['ђ', 'đ'],
  392. 'dz' => ['џ', 'ძ'],
  393. 'ei' => ['ऍ'],
  394. 'gh' => ['غ', 'ღ'],
  395. 'ii' => ['ई'],
  396. 'ij' => ['ij'],
  397. 'kh' => ['х', 'خ', 'ხ'],
  398. 'lj' => ['љ'],
  399. 'nj' => ['њ'],
  400. 'oe' => ['ö', 'œ', 'ؤ'],
  401. 'oi' => ['ऑ'],
  402. 'oii' => ['ऒ'],
  403. 'ps' => ['ψ'],
  404. 'sh' => ['ш', 'შ', 'ش'],
  405. 'shch' => ['щ'],
  406. 'ss' => ['ß'],
  407. 'sx' => ['ŝ'],
  408. 'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
  409. 'ts' => ['ц', 'ც', 'წ'],
  410. 'ue' => ['ü'],
  411. 'uu' => ['ऊ'],
  412. 'ya' => ['я'],
  413. 'yu' => ['ю'],
  414. 'zh' => ['ж', 'ჟ', 'ژ'],
  415. '(c)' => ['©'],
  416. 'A' => ['Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А', 'Ǻ', 'Ǎ', 'A', 'Ä'],
  417. 'B' => ['Б', 'Β', 'ब', 'B'],
  418. 'C' => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ', 'C'],
  419. 'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ', 'D'],
  420. 'E' => ['É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ', 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё', 'Э', 'Є', 'Ə', 'E'],
  421. 'F' => ['Ф', 'Φ', 'F'],
  422. 'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ', 'G'],
  423. 'H' => ['Η', 'Ή', 'Ħ', 'H'],
  424. 'I' => ['Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į', 'İ', 'Ι', 'Ί', 'Ϊ', 'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И', 'І', 'Ї', 'Ǐ', 'ϒ', 'I'],
  425. 'J' => ['J'],
  426. 'K' => ['К', 'Κ', 'K'],
  427. 'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल', 'L'],
  428. 'M' => ['М', 'Μ', 'M'],
  429. 'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν', 'N'],
  430. 'O' => ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О', 'Θ', 'Ө', 'Ǒ', 'Ǿ', 'O', 'Ö'],
  431. 'P' => ['П', 'Π', 'P'],
  432. 'Q' => ['Q'],
  433. 'R' => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ', 'R'],
  434. 'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ', 'S'],
  435. 'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ', 'T'],
  436. 'U' => ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Û', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ų', 'У', 'Ǔ', 'Ǖ', 'Ǘ', 'Ǚ', 'Ǜ', 'U', 'Ў', 'Ü'],
  437. 'V' => ['В', 'V'],
  438. 'W' => ['Ω', 'Ώ', 'Ŵ', 'W'],
  439. 'X' => ['Χ', 'Ξ', 'X'],
  440. 'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', 'Y'],
  441. 'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ', 'Z'],
  442. 'AE' => ['Æ', 'Ǽ'],
  443. 'Ch' => ['Ч'],
  444. 'Dj' => ['Ђ'],
  445. 'Dz' => ['Џ'],
  446. 'Gx' => ['Ĝ'],
  447. 'Hx' => ['Ĥ'],
  448. 'Ij' => ['IJ'],
  449. 'Jx' => ['Ĵ'],
  450. 'Kh' => ['Х'],
  451. 'Lj' => ['Љ'],
  452. 'Nj' => ['Њ'],
  453. 'Oe' => ['Œ'],
  454. 'Ps' => ['Ψ'],
  455. 'Sh' => ['Ш'],
  456. 'Shch' => ['Щ'],
  457. 'Ss' => ['ẞ'],
  458. 'Th' => ['Þ'],
  459. 'Ts' => ['Ц'],
  460. 'Ya' => ['Я'],
  461. 'Yu' => ['Ю'],
  462. 'Zh' => ['Ж'],
  463. ' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80", "\xEF\xBE\xA0"],
  464. ];
  465. }
  466. /**
  467. * Returns the language specific replacements for the ascii method.
  468. *
  469. * Note: Adapted from Stringy\Stringy.
  470. *
  471. * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
  472. */
  473. protected static function languageSpecificCharsArray(string $language): ?array
  474. {
  475. static $languageSpecific;
  476. if (!isset($languageSpecific)) {
  477. $languageSpecific = [
  478. 'bg' => [
  479. ['х', 'Х', 'щ', 'Щ', 'ъ', 'Ъ', 'ь', 'Ь'],
  480. ['h', 'H', 'sht', 'SHT', 'a', 'А', 'y', 'Y'],
  481. ],
  482. 'de' => [
  483. ['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'],
  484. ['ae', 'oe', 'ue', 'AE', 'OE', 'UE'],
  485. ],
  486. ];
  487. }
  488. return isset($languageSpecific[$language]) ? $languageSpecific[$language] : null;
  489. }
  490. }