Str.php 22 KB

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