Str.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Stringable;
  12. use Closure;
  13. use Countable;
  14. use DateTimeInterface;
  15. use Hyperf\Collection\Arr;
  16. use Hyperf\Collection\Collection;
  17. use Hyperf\Macroable\Macroable;
  18. use InvalidArgumentException;
  19. use JsonException;
  20. use Ramsey\Uuid\Uuid;
  21. use Ramsey\Uuid\UuidInterface;
  22. use RuntimeException;
  23. use Symfony\Component\Uid\Ulid;
  24. use function Hyperf\Collection\collect;
  25. /**
  26. * Most of the methods in this file come from illuminate/support,
  27. * thanks Laravel Team provide such a useful class.
  28. */
  29. class Str
  30. {
  31. use Macroable;
  32. /**
  33. * Get a new stringable object from the given string.
  34. *
  35. * @param string $string
  36. * @return Stringable
  37. */
  38. public static function of($string)
  39. {
  40. return new Stringable($string);
  41. }
  42. /**
  43. * Return the remainder of a string after a given value.
  44. *
  45. * @param string $subject
  46. * @param string $search
  47. * @return string
  48. */
  49. public static function after($subject, $search)
  50. {
  51. return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
  52. }
  53. /**
  54. * Return the remainder of a string after the last occurrence of a given value.
  55. *
  56. * @param string $subject
  57. * @param string $search
  58. * @return string
  59. */
  60. public static function afterLast($subject, $search)
  61. {
  62. if ($search === '') {
  63. return $subject;
  64. }
  65. $position = strrpos($subject, (string) $search);
  66. if ($position === false) {
  67. return $subject;
  68. }
  69. return substr($subject, $position + strlen($search));
  70. }
  71. /**
  72. * Convert the given string to APA-style title case.
  73. *
  74. * See: https://apastyle.apa.org/style-grammar-guidelines/capitalization/title-case
  75. *
  76. * @param string $value
  77. * @return string
  78. */
  79. public static function apa($value)
  80. {
  81. if (trim($value) === '') {
  82. return $value;
  83. }
  84. $minorWords = [
  85. 'and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an',
  86. 'the', 'at', 'by', 'for', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via',
  87. ];
  88. $endPunctuation = ['.', '!', '?', ':', '—', ','];
  89. $words = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY);
  90. $words[0] = ucfirst(mb_strtolower($words[0]));
  91. for ($i = 0; $i < count($words); ++$i) {
  92. $lowercaseWord = mb_strtolower($words[$i]);
  93. if (str_contains($lowercaseWord, '-')) {
  94. $hyphenatedWords = explode('-', $lowercaseWord);
  95. $hyphenatedWords = array_map(function ($part) use ($minorWords) {
  96. return (in_array($part, $minorWords)) ? $part : ucfirst($part);
  97. }, $hyphenatedWords);
  98. $words[$i] = implode('-', $hyphenatedWords);
  99. } else {
  100. if (in_array($lowercaseWord, $minorWords)
  101. && ! ($i === 0 || in_array(mb_substr($words[$i - 1], -1), $endPunctuation))) {
  102. $words[$i] = $lowercaseWord;
  103. } else {
  104. $words[$i] = ucfirst($lowercaseWord);
  105. }
  106. }
  107. }
  108. return implode(' ', $words);
  109. }
  110. /**
  111. * Transliterate a UTF-8 value to ASCII.
  112. *
  113. * @param string $value
  114. * @param string $language
  115. * @return string
  116. */
  117. public static function ascii($value, $language = 'en')
  118. {
  119. $languageSpecific = static::languageSpecificCharsArray($language);
  120. if (! is_null($languageSpecific)) {
  121. $value = str_replace($languageSpecific[0], $languageSpecific[1], $value);
  122. }
  123. foreach (static::charsArray() as $key => $val) {
  124. $value = str_replace($val, (string) $key, $value);
  125. }
  126. return preg_replace('/[^\x20-\x7E]/u', '', $value);
  127. }
  128. /**
  129. * Get the portion of a string before a given value.
  130. *
  131. * @param string $subject
  132. * @param string $search
  133. * @return string
  134. */
  135. public static function before($subject, $search)
  136. {
  137. return $search === '' ? $subject : explode($search, $subject)[0];
  138. }
  139. /**
  140. * Get the portion of a string before the last occurrence of a given value.
  141. *
  142. * @param string $subject
  143. * @param string $search
  144. * @return string
  145. */
  146. public static function beforeLast($subject, $search)
  147. {
  148. if ($search === '') {
  149. return $subject;
  150. }
  151. $pos = mb_strrpos($subject, $search);
  152. if ($pos === false) {
  153. return $subject;
  154. }
  155. return static::substr($subject, 0, $pos);
  156. }
  157. /**
  158. * Get the portion of a string between two given values.
  159. *
  160. * @param string $subject
  161. * @param string $from
  162. * @param string $to
  163. * @return string
  164. */
  165. public static function between($subject, $from, $to)
  166. {
  167. if ($from === '' || $to === '') {
  168. return $subject;
  169. }
  170. return static::beforeLast(static::after($subject, $from), $to);
  171. }
  172. /**
  173. * Convert a value to camel case.
  174. *
  175. * @param string $value
  176. * @return string
  177. */
  178. public static function camel($value)
  179. {
  180. return lcfirst(static::studly($value));
  181. }
  182. /**
  183. * Get the character at the specified index.
  184. *
  185. * @param string $subject
  186. * @param int $index
  187. * @return null|string
  188. */
  189. public static function charAt($subject, $index)
  190. {
  191. $length = mb_strlen($subject);
  192. if ($index < 0 ? $index < -$length : $index > $length - 1) {
  193. return null;
  194. }
  195. return mb_substr($subject, $index, 1);
  196. }
  197. /**
  198. * Remove the given string(s) if it exists at the end of the haystack.
  199. *
  200. * @param string $subject
  201. * @param array|string $needle
  202. * @return string
  203. */
  204. public static function chopEnd($subject, $needle)
  205. {
  206. foreach ((array) $needle as $n) {
  207. if (str_ends_with($subject, $n)) {
  208. return substr($subject, 0, -strlen($n));
  209. }
  210. }
  211. return $subject;
  212. }
  213. /**
  214. * Remove the given string(s) if it exists at the start of the haystack.
  215. *
  216. * @param string $subject
  217. * @param array|string $needle
  218. * @return string
  219. */
  220. public static function chopStart($subject, $needle)
  221. {
  222. foreach ((array) $needle as $n) {
  223. if (str_starts_with($subject, $n)) {
  224. return substr($subject, strlen($n));
  225. }
  226. }
  227. return $subject;
  228. }
  229. /**
  230. * Determine if a given string contains a given substring.
  231. *
  232. * @param array|string $needles
  233. */
  234. public static function contains(string $haystack, mixed $needles, bool $ignoreCase = false): bool
  235. {
  236. if ($ignoreCase) {
  237. return static::containsIgnoreCase($haystack, $needles);
  238. }
  239. foreach ((array) $needles as $needle) {
  240. $needle = (string) $needle;
  241. if ($needle !== '' && str_contains($haystack, $needle)) {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. /**
  248. * Determine if a given string contains a given substring regardless of case sensitivity.
  249. *
  250. * @param array|string $needles
  251. */
  252. public static function containsIgnoreCase(string $haystack, $needles): bool
  253. {
  254. foreach ((array) $needles as $needle) {
  255. $needle = (string) $needle;
  256. if ($needle !== '' && stripos($haystack, $needle) !== false) {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. /**
  263. * Determine if a given string contains all array values.
  264. *
  265. * @param string[] $needles
  266. * @return bool
  267. */
  268. public static function containsAll(string $haystack, array $needles, bool $ignoreCase = false)
  269. {
  270. foreach ($needles as $needle) {
  271. if (! static::contains($haystack, $needle, $ignoreCase)) {
  272. return false;
  273. }
  274. }
  275. return true;
  276. }
  277. /**
  278. * Determine if a given string ends with a given substring.
  279. *
  280. * @param array|string $needles
  281. * @return bool
  282. */
  283. public static function endsWith(string $haystack, $needles)
  284. {
  285. foreach ((array) $needles as $needle) {
  286. $needle = (string) $needle;
  287. if ($needle !== '' && str_ends_with($haystack, $needle)) {
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. /**
  294. * Cap a string with a single instance of a given value.
  295. *
  296. * @param string $value
  297. * @param string $cap
  298. * @return string
  299. */
  300. public static function finish($value, $cap)
  301. {
  302. $quoted = preg_quote($cap, '/');
  303. return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap;
  304. }
  305. /**
  306. * Determine if a given string matches a given pattern.
  307. *
  308. * @param array|string $pattern
  309. * @param string $value
  310. * @param bool $ignoreCase
  311. * @return bool
  312. */
  313. public static function is($pattern, $value, $ignoreCase = false)
  314. {
  315. $value = (string) $value;
  316. $patterns = Arr::wrap($pattern);
  317. if (empty($patterns)) {
  318. return false;
  319. }
  320. foreach ($patterns as $pattern) {
  321. $pattern = (string) $pattern;
  322. // If the given value is an exact match we can of course return true right
  323. // from the beginning. Otherwise, we will translate asterisks and do an
  324. // actual pattern match against the two strings to see if they match.
  325. if ($pattern == $value) {
  326. return true;
  327. }
  328. $pattern = preg_quote($pattern, '#');
  329. // Asterisks are translated into zero-or-more regular expression wildcards
  330. // to make it convenient to check if the strings starts with the given
  331. // pattern such as "library/*", making any string check convenient.
  332. $pattern = str_replace('\*', '.*', $pattern);
  333. if (preg_match('#^' . $pattern . '\z#' . ($ignoreCase ? 'iu' : 'u'), $value) === 1) {
  334. return true;
  335. }
  336. }
  337. return false;
  338. }
  339. /**
  340. * Determine if a given string is 7 bit ASCII.
  341. */
  342. public static function isAscii(string $value): bool
  343. {
  344. if ($value == '') {
  345. return true;
  346. }
  347. return ! preg_match('/[^\x09\x0A\x0D\x20-\x7E]/', $value);
  348. }
  349. /**
  350. * Convert a string to kebab case.
  351. *
  352. * @param string $value
  353. * @return string
  354. */
  355. public static function kebab($value)
  356. {
  357. return static::snake($value, '-');
  358. }
  359. /**
  360. * Return the length of the given string.
  361. *
  362. * @param string $value
  363. * @param string $encoding
  364. * @return int
  365. */
  366. public static function length($value, $encoding = null)
  367. {
  368. return mb_strlen($value, $encoding);
  369. }
  370. /**
  371. * Limit the number of characters in a string.
  372. *
  373. * @param string $value
  374. * @param int $limit
  375. * @param string $end
  376. * @return string
  377. */
  378. public static function limit($value, $limit = 100, $end = '...')
  379. {
  380. if (mb_strwidth($value, 'UTF-8') <= $limit) {
  381. return $value;
  382. }
  383. return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')) . $end;
  384. }
  385. /**
  386. * Convert the given string to lower-case.
  387. *
  388. * @param string $value
  389. * @return string
  390. */
  391. public static function lower($value)
  392. {
  393. return mb_strtolower($value, 'UTF-8');
  394. }
  395. /**
  396. * Limit the number of words in a string.
  397. */
  398. public static function words(string $value, int $words = 100, string $end = '...'): string
  399. {
  400. preg_match('/^\s*+(?:\S++\s*+){1,' . $words . '}/u', $value, $matches);
  401. if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  402. return $value;
  403. }
  404. return rtrim($matches[0]) . $end;
  405. }
  406. /**
  407. * Get the string matching the given pattern.
  408. *
  409. * @param string $pattern
  410. * @param string $subject
  411. * @return string
  412. */
  413. public static function match($pattern, $subject)
  414. {
  415. preg_match($pattern, $subject, $matches);
  416. if (! $matches) {
  417. return '';
  418. }
  419. return $matches[1] ?? $matches[0];
  420. }
  421. /**
  422. * Determine if a given string matches a given pattern.
  423. *
  424. * @param iterable<string>|string $patterns
  425. * @param string $value
  426. * @return bool
  427. */
  428. public static function isMatch($patterns, $value)
  429. {
  430. $value = (string) $value;
  431. if (! is_iterable($patterns)) {
  432. $patterns = [$patterns];
  433. }
  434. foreach ($patterns as $pattern) {
  435. $pattern = (string) $pattern;
  436. if (preg_match($pattern, $value) === 1) {
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. /**
  443. * Get the string matching the given pattern.
  444. *
  445. * @param string $pattern
  446. * @param string $subject
  447. * @return Collection
  448. */
  449. public static function matchAll($pattern, $subject)
  450. {
  451. preg_match_all($pattern, $subject, $matches);
  452. if (empty($matches[0])) {
  453. return collect();
  454. }
  455. return collect($matches[1] ?? $matches[0]);
  456. }
  457. /**
  458. * Pad both sides of a string with another.
  459. *
  460. * @param string $value
  461. * @param int $length
  462. * @param string $pad
  463. * @return string
  464. */
  465. public static function padBoth($value, $length, $pad = ' ')
  466. {
  467. return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_BOTH);
  468. }
  469. /**
  470. * Pad the left side of a string with another.
  471. *
  472. * @param string $value
  473. * @param int $length
  474. * @param string $pad
  475. * @return string
  476. */
  477. public static function padLeft($value, $length, $pad = ' ')
  478. {
  479. return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_LEFT);
  480. }
  481. /**
  482. * Pad the right side of a string with another.
  483. *
  484. * @param string $value
  485. * @param int $length
  486. * @param string $pad
  487. * @return string
  488. */
  489. public static function padRight($value, $length, $pad = ' ')
  490. {
  491. return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_RIGHT);
  492. }
  493. /**
  494. * Parse a Class@method style callback into class and method.
  495. *
  496. * @param null|string $default
  497. */
  498. public static function parseCallback(string $callback, $default = null): array
  499. {
  500. return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
  501. }
  502. /**
  503. * Pluralize the last word of an English, studly caps case string.
  504. * @param mixed $count
  505. */
  506. public static function pluralStudly(string $value, $count = 2): string
  507. {
  508. $parts = preg_split('/(.)(?=[A-Z])/u', $value, -1, PREG_SPLIT_DELIM_CAPTURE);
  509. $lastWord = array_pop($parts);
  510. return implode('', $parts) . self::plural($lastWord, $count);
  511. }
  512. /**
  513. * Get the plural form of an English word.
  514. */
  515. public static function plural(string $value, array|Countable|int $count = 2): string
  516. {
  517. return Pluralizer::plural($value, $count);
  518. }
  519. /**
  520. * Find the multi-byte safe position of the first occurrence of a given substring in a string.
  521. */
  522. public static function position(string $haystack, string $needle, int $offset = 0, ?string $encoding = null): false|int
  523. {
  524. return mb_strpos($haystack, (string) $needle, $offset, $encoding);
  525. }
  526. /**
  527. * Generate a more truly "random" alpha-numeric string.
  528. */
  529. public static function random(int $length = 16): string
  530. {
  531. $string = '';
  532. while (($len = strlen($string)) < $length) {
  533. $size = $length - $len;
  534. $bytes = random_bytes($size);
  535. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  536. }
  537. return $string;
  538. }
  539. /**
  540. * Repeat the given string.
  541. *
  542. * @return string
  543. */
  544. public static function repeat(string $string, int $times)
  545. {
  546. return str_repeat($string, $times);
  547. }
  548. /**
  549. * Replace a given value in the string sequentially with an array.
  550. *
  551. * @param string[] $replace
  552. */
  553. public static function replaceArray(string $search, array $replace, string $subject): string
  554. {
  555. foreach ($replace as $value) {
  556. $subject = static::replaceFirst($search, (string) $value, $subject);
  557. }
  558. return $subject;
  559. }
  560. /**
  561. * Replace the given value in the given string.
  562. *
  563. * @param string|string[] $search
  564. * @param string|string[] $replace
  565. * @param string|string[] $subject
  566. * @return string
  567. */
  568. public static function replace($search, $replace, $subject)
  569. {
  570. return str_replace($search, $replace, $subject);
  571. }
  572. /**
  573. * Replace the first occurrence of a given value in the string.
  574. */
  575. public static function replaceFirst(string $search, string $replace, string $subject): string
  576. {
  577. if ($search == '') {
  578. return $subject;
  579. }
  580. $position = strpos($subject, $search);
  581. if ($position !== false) {
  582. return substr_replace($subject, $replace, $position, strlen($search));
  583. }
  584. return $subject;
  585. }
  586. /**
  587. * Replace the last occurrence of a given value in the string.
  588. */
  589. public static function replaceLast(string $search, string $replace, string $subject): string
  590. {
  591. if ($search == '') {
  592. return $subject;
  593. }
  594. $position = strrpos($subject, $search);
  595. if ($position !== false) {
  596. return substr_replace($subject, $replace, $position, strlen($search));
  597. }
  598. return $subject;
  599. }
  600. /**
  601. * Remove any occurrence of the given string in the subject.
  602. *
  603. * @param array<string>|string $search
  604. * @param string $subject
  605. * @param bool $caseSensitive
  606. * @return string
  607. */
  608. public static function remove($search, $subject, $caseSensitive = true)
  609. {
  610. return $caseSensitive
  611. ? str_replace($search, '', $subject)
  612. : str_ireplace($search, '', $subject);
  613. }
  614. /**
  615. * Begin a string with a single instance of a given value.
  616. */
  617. public static function start(string $value, string $prefix): string
  618. {
  619. $quoted = preg_quote($prefix, '/');
  620. return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
  621. }
  622. /**
  623. * Strip HTML and PHP tags from the given string.
  624. *
  625. * @param null|string|string[] $allowedTags
  626. */
  627. public static function stripTags(string $value, $allowedTags = null): string
  628. {
  629. return strip_tags($value, $allowedTags);
  630. }
  631. /**
  632. * Convert the given string to upper-case.
  633. */
  634. public static function upper(string $value): string
  635. {
  636. return mb_strtoupper($value, 'UTF-8');
  637. }
  638. /**
  639. * Convert the given string to title case.
  640. */
  641. public static function title(string $value): string
  642. {
  643. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  644. }
  645. /**
  646. * Convert the given string to proper case for each word.
  647. */
  648. public static function headline(string $value): string
  649. {
  650. $parts = explode(' ', $value);
  651. $parts = count($parts) > 1
  652. ? array_map([static::class, 'title'], $parts)
  653. : array_map([static::class, 'title'], static::ucsplit(implode('_', $parts)));
  654. $collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts));
  655. return implode(' ', array_filter(explode('_', $collapsed)));
  656. }
  657. /**
  658. * Get the singular form of an English word.
  659. */
  660. public static function singular(string $value): string
  661. {
  662. return Pluralizer::singular($value);
  663. }
  664. /**
  665. * Generate a URL friendly "slug" from a given string.
  666. * @param mixed $dictionary
  667. */
  668. public static function slug(string $title, string $separator = '-', ?string $language = 'en', $dictionary = ['@' => 'at']): string
  669. {
  670. $title = $language ? static::ascii($title, $language) : $title;
  671. // Convert all dashes/underscores into separator
  672. $flip = $separator === '-' ? '_' : '-';
  673. $title = preg_replace('![' . preg_quote($flip) . ']+!u', $separator, $title);
  674. // Replace dictionary words
  675. foreach ($dictionary as $key => $value) {
  676. $dictionary[$key] = $separator . $value . $separator;
  677. }
  678. $title = str_replace(array_keys($dictionary), array_values($dictionary), $title);
  679. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  680. $title = preg_replace('![^' . preg_quote($separator) . '\pL\pN\s]+!u', '', mb_strtolower($title));
  681. // Replace all separator characters and whitespace by a single separator
  682. $title = preg_replace('![' . preg_quote($separator) . '\s]+!u', $separator, $title);
  683. return trim($title, $separator);
  684. }
  685. /**
  686. * Convert a string to snake case.
  687. */
  688. public static function snake(string $value, string $delimiter = '_'): string
  689. {
  690. if (! ctype_lower($value)) {
  691. $value = preg_replace('/\s+/u', '', ucwords($value));
  692. $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
  693. }
  694. return $value;
  695. }
  696. /**
  697. * Determine if a given string starts with a given substring.
  698. *
  699. * @param array|string $needles
  700. */
  701. public static function startsWith(string $haystack, $needles): bool
  702. {
  703. foreach ((array) $needles as $needle) {
  704. $needle = (string) $needle;
  705. if ($needle !== '' && str_starts_with($haystack, $needle)) {
  706. return true;
  707. }
  708. }
  709. return false;
  710. }
  711. /**
  712. * Convert a value to studly caps case.
  713. */
  714. public static function studly(string $value, string $gap = ''): string
  715. {
  716. $value = ucwords(str_replace(['-', '_'], ' ', $value));
  717. return str_replace(' ', $gap, $value);
  718. }
  719. /**
  720. * Returns the portion of string specified by the start and length parameters.
  721. *
  722. * @param string $string
  723. * @param int $start
  724. * @param null|int $length
  725. * @return string
  726. */
  727. public static function substr($string, $start, $length = null)
  728. {
  729. return mb_substr($string, $start, $length, 'UTF-8');
  730. }
  731. /**
  732. * Returns the number of substring occurrences.
  733. *
  734. * @param string $haystack
  735. * @param string $needle
  736. * @param int $offset
  737. * @param null|int $length
  738. * @return int
  739. */
  740. public static function substrCount($haystack, $needle, $offset = 0, $length = null)
  741. {
  742. if (! is_null($length)) {
  743. return substr_count($haystack, $needle, $offset, $length);
  744. }
  745. return substr_count($haystack, $needle, $offset);
  746. }
  747. /**
  748. * Make a string's first character uppercase.
  749. */
  750. public static function ucfirst(string $string): string
  751. {
  752. return static::upper(static::substr($string, 0, 1)) . static::substr($string, 1);
  753. }
  754. /**
  755. * Replaces the first or the last ones chars from a string by a given char.
  756. *
  757. * @param int $offset if is negative it starts from the end
  758. * @param string $replacement default is *
  759. * @return string
  760. */
  761. public static function mask(string $string, int $offset = 0, int $length = 0, string $replacement = '*')
  762. {
  763. if ($length < 0) {
  764. throw new InvalidArgumentException('The length must equal or greater than zero.');
  765. }
  766. $stringLength = mb_strlen($string);
  767. $absOffset = abs($offset);
  768. if ($absOffset >= $stringLength) {
  769. return $string;
  770. }
  771. $hiddenLength = $length ?: $stringLength - $absOffset;
  772. if ($offset >= 0) {
  773. return mb_substr($string, 0, $offset) . str_repeat($replacement, $hiddenLength) . mb_substr($string, $offset + $hiddenLength);
  774. }
  775. return mb_substr($string, 0, max($stringLength - $hiddenLength - $absOffset, 0)) . str_repeat($replacement, $hiddenLength) . mb_substr($string, $offset);
  776. }
  777. /**
  778. * Determine if a given value is a valid ULID.
  779. *
  780. * @param mixed $value
  781. */
  782. public static function isUlid($value): bool
  783. {
  784. if (! is_string($value)) {
  785. return false;
  786. }
  787. if (strlen($value) !== 26) {
  788. return false;
  789. }
  790. if (strspn($value, '0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz') !== 26) {
  791. return false;
  792. }
  793. return $value[0] <= '7';
  794. }
  795. /**
  796. * Generate a ULID.
  797. */
  798. public static function ulid(?DateTimeInterface $time = null): Ulid
  799. {
  800. if (! class_exists(Ulid::class)) {
  801. throw new RuntimeException('The "symfony/uid" package is required to use the "ulid" method. Please run "composer require symfony/uid".');
  802. }
  803. return new Ulid(Ulid::generate($time));
  804. }
  805. /**
  806. * Determine if a given value is a valid URL.
  807. *
  808. * @param string $value
  809. */
  810. public static function isUrl($value, array $protocols = []): bool
  811. {
  812. if (! is_string($value)) {
  813. return false;
  814. }
  815. $protocolList = empty($protocols)
  816. ? 'aaa|aaas|about|acap|acct|acd|acr|adiumxtra|adt|afp|afs|aim|amss|android|appdata|apt|ark|attachment|aw|barion|beshare|bitcoin|bitcoincash|blob|bolo|browserext|calculator|callto|cap|cast|casts|chrome|chrome-extension|cid|coap|coap\+tcp|coap\+ws|coaps|coaps\+tcp|coaps\+ws|com-eventbrite-attendee|content|conti|crid|cvs|dab|data|dav|diaspora|dict|did|dis|dlna-playcontainer|dlna-playsingle|dns|dntp|dpp|drm|drop|dtn|dvb|ed2k|elsi|example|facetime|fax|feed|feedready|file|filesystem|finger|first-run-pen-experience|fish|fm|ftp|fuchsia-pkg|geo|gg|git|gizmoproject|go|gopher|graph|gtalk|h323|ham|hcap|hcp|http|https|hxxp|hxxps|hydrazone|iax|icap|icon|im|imap|info|iotdisco|ipn|ipp|ipps|irc|irc6|ircs|iris|iris\.beep|iris\.lwz|iris\.xpc|iris\.xpcs|isostore|itms|jabber|jar|jms|keyparc|lastfm|ldap|ldaps|leaptofrogans|lorawan|lvlt|magnet|mailserver|mailto|maps|market|message|mid|mms|modem|mongodb|moz|ms-access|ms-browser-extension|ms-calculator|ms-drive-to|ms-enrollment|ms-excel|ms-eyecontrolspeech|ms-gamebarservices|ms-gamingoverlay|ms-getoffice|ms-help|ms-infopath|ms-inputapp|ms-lockscreencomponent-config|ms-media-stream-id|ms-mixedrealitycapture|ms-mobileplans|ms-officeapp|ms-people|ms-project|ms-powerpoint|ms-publisher|ms-restoretabcompanion|ms-screenclip|ms-screensketch|ms-search|ms-search-repair|ms-secondary-screen-controller|ms-secondary-screen-setup|ms-settings|ms-settings-airplanemode|ms-settings-bluetooth|ms-settings-camera|ms-settings-cellular|ms-settings-cloudstorage|ms-settings-connectabledevices|ms-settings-displays-topology|ms-settings-emailandaccounts|ms-settings-language|ms-settings-location|ms-settings-lock|ms-settings-nfctransactions|ms-settings-notifications|ms-settings-power|ms-settings-privacy|ms-settings-proximity|ms-settings-screenrotation|ms-settings-wifi|ms-settings-workplace|ms-spd|ms-sttoverlay|ms-transit-to|ms-useractivityset|ms-virtualtouchpad|ms-visio|ms-walk-to|ms-whiteboard|ms-whiteboard-cmd|ms-word|msnim|msrp|msrps|mss|mtqp|mumble|mupdate|mvn|news|nfs|ni|nih|nntp|notes|ocf|oid|onenote|onenote-cmd|opaquelocktoken|openpgp4fpr|pack|palm|paparazzi|payto|pkcs11|platform|pop|pres|prospero|proxy|pwid|psyc|pttp|qb|query|redis|rediss|reload|res|resource|rmi|rsync|rtmfp|rtmp|rtsp|rtsps|rtspu|s3|secondlife|service|session|sftp|sgn|shttp|sieve|simpleledger|sip|sips|skype|smb|sms|smtp|snews|snmp|soap\.beep|soap\.beeps|soldat|spiffe|spotify|ssh|steam|stun|stuns|submit|svn|tag|teamspeak|tel|teliaeid|telnet|tftp|tg|things|thismessage|tip|tn3270|tool|ts3server|turn|turns|tv|udp|unreal|urn|ut2004|v-event|vemmi|ventrilo|videotex|vnc|view-source|wais|webcal|wpid|ws|wss|wtai|wyciwyg|xcon|xcon-userid|xfire|xmlrpc\.beep|xmlrpc\.beeps|xmpp|xri|ymsgr|z39\.50|z39\.50r|z39\.50s'
  817. : implode('|', $protocols);
  818. /*
  819. * This pattern is derived from Symfony\Component\Validator\Constraints\UrlValidator (5.0.7).
  820. *
  821. * (c) Fabien Potencier <fabien@symfony.com> http://symfony.com
  822. */
  823. $pattern = '~^
  824. (DEFAULT_PROTOCOLS):// # protocol
  825. (((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+:)?((?:[\_\.\pL\pN-]|%[0-9A-Fa-f]{2})+)@)? # basic auth
  826. (
  827. ([\pL\pN\pS\-\_\.])+(\.?([\pL\pN]|xn\-\-[\pL\pN-]+)+\.?) # a domain name
  828. | # or
  829. \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # an IP address
  830. | # or
  831. \[
  832. (?:(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){6})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?:(?:[0-9a-f]{1,4})):){5})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){4})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,1}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){3})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,2}(?:(?:[0-9a-f]{1,4})))?::(?:(?:(?:[0-9a-f]{1,4})):){2})(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,3}(?:(?:[0-9a-f]{1,4})))?::(?:(?:[0-9a-f]{1,4})):)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,4}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:(?:(?:(?:[0-9a-f]{1,4})):(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,5}(?:(?:[0-9a-f]{1,4})))?::)(?:(?:[0-9a-f]{1,4})))|(?:(?:(?:(?:(?:(?:[0-9a-f]{1,4})):){0,6}(?:(?:[0-9a-f]{1,4})))?::))))
  833. \] # an IPv6 address
  834. )
  835. (:[0-9]+)? # a port (optional)
  836. (?:/ (?:[\pL\pN\-._\~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})* )* # a path
  837. (?:\? (?:[\pL\pN\-._\~!$&\'\[\]()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a query (optional)
  838. (?:\# (?:[\pL\pN\-._\~!$&\'()*+,;=:@/?]|%[0-9A-Fa-f]{2})* )? # a fragment (optional)
  839. $~ixu';
  840. return preg_match(str_replace('DEFAULT_PROTOCOLS', $protocolList, $pattern), $value) > 0;
  841. }
  842. /**
  843. * Determine if a given value is a valid UUID.
  844. *
  845. * @param mixed $value
  846. */
  847. public static function isUuid($value): bool
  848. {
  849. if (! is_string($value)) {
  850. return false;
  851. }
  852. return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;
  853. }
  854. /**
  855. * Generate a UUID (version 4).
  856. */
  857. public static function uuid(): UuidInterface
  858. {
  859. if (! class_exists(Uuid::class)) {
  860. throw new RuntimeException('The "ramsey/uuid" package is required to use the "uuid" method. Please run "composer require ramsey/uuid".');
  861. }
  862. return Uuid::uuid4();
  863. }
  864. /**
  865. * Generate a time-ordered UUID.
  866. */
  867. public static function orderedUuid(?DateTimeInterface $time = null): UuidInterface
  868. {
  869. if (! class_exists(Uuid::class)) {
  870. throw new RuntimeException('The "ramsey/uuid" package is required to use the "orderedUuid" method. Please run "composer require ramsey/uuid".');
  871. }
  872. return Uuid::uuid7($time);
  873. }
  874. /**
  875. * Get the smallest possible portion of a string between two given values.
  876. *
  877. * @param string $subject
  878. * @param string $from
  879. * @param string $to
  880. * @return string
  881. */
  882. public static function betweenFirst($subject, $from, $to)
  883. {
  884. if ($from === '' || $to === '') {
  885. return $subject;
  886. }
  887. return Str::before(Str::after($subject, $from), $to);
  888. }
  889. /**
  890. * @param string $value
  891. */
  892. public static function classNamespace($value): string
  893. {
  894. if ($pos = strrpos($value, '\\')) {
  895. return substr($value, 0, $pos);
  896. }
  897. return '';
  898. }
  899. /**
  900. * Convert the case of a string.
  901. */
  902. public static function convertCase(string $string, int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8'): string
  903. {
  904. return mb_convert_case($string, $mode, $encoding);
  905. }
  906. /**
  907. * Extracts an excerpt from text that matches the first instance of a phrase.
  908. *
  909. * @param string $text
  910. * @param string $phrase
  911. * @param array $options
  912. * @return null|string
  913. */
  914. public static function excerpt($text, $phrase = '', $options = [])
  915. {
  916. $radius = $options['radius'] ?? 100;
  917. $omission = $options['omission'] ?? '...';
  918. preg_match('/^(.*?)(' . preg_quote((string) $phrase) . ')(.*)$/iu', (string) $text, $matches);
  919. if (empty($matches)) {
  920. return null;
  921. }
  922. $startStr = ltrim($matches[1]);
  923. $start = Str::of(mb_substr($matches[1], max(mb_strlen($startStr, 'UTF-8') - $radius, 0), $radius, 'UTF-8'))->ltrim();
  924. $start = $start->unless(
  925. (fn ($startWithRadius) => $startWithRadius->exactly($startStr))($start),
  926. fn ($startWithRadius) => $startWithRadius->prepend($omission),
  927. );
  928. $endStr = rtrim($matches[3]);
  929. $end = Str::of(mb_substr($endStr, 0, $radius, 'UTF-8'))->rtrim();
  930. $end = $end->unless(
  931. (fn ($endWithRadius) => $endWithRadius->exactly($endStr))($end),
  932. fn ($endWithRadius) => $endWithRadius->append($omission),
  933. );
  934. return $start->append($matches[2], (string) $end)->__toString();
  935. }
  936. /**
  937. * Determine if a given value is valid JSON.
  938. *
  939. * @param mixed $value
  940. */
  941. public static function isJson($value): bool
  942. {
  943. if (! is_string($value)) {
  944. return false;
  945. }
  946. if (function_exists('json_validate')) {
  947. return json_validate($value, 512);
  948. }
  949. try {
  950. json_decode($value, true, 512, JSON_THROW_ON_ERROR);
  951. } catch (JsonException $e) {
  952. return false;
  953. }
  954. return true;
  955. }
  956. /**
  957. * Make a string's first character lowercase.
  958. *
  959. * @param string $string
  960. */
  961. public static function lcfirst($string): string
  962. {
  963. return Str::lower(Str::substr($string, 0, 1)) . Str::substr($string, 1);
  964. }
  965. /**
  966. * Generate a random, secure password.
  967. *
  968. * @param int $length
  969. * @param bool $letters
  970. * @param bool $numbers
  971. * @param bool $symbols
  972. * @param bool $spaces
  973. * @return string
  974. */
  975. public static function password($length = 32, $letters = true, $numbers = true, $symbols = true, $spaces = false)
  976. {
  977. return (new Collection())
  978. ->when($letters, fn ($c) => $c->merge([
  979. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
  980. 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  981. 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  982. 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  983. 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  984. ]))
  985. ->when($numbers, fn ($c) => $c->merge([
  986. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  987. ]))
  988. ->when($symbols, fn ($c) => $c->merge([
  989. '~', '!', '#', '$', '%', '^', '&', '*', '(', ')', '-',
  990. '_', '.', ',', '<', '>', '?', '/', '\\', '{', '}', '[',
  991. ']', '|', ':', ';',
  992. ]))
  993. ->when($spaces, fn ($c) => $c->merge([' ']))
  994. ->pipe(fn ($c) => Collection::times($length, fn () => $c[random_int(0, $c->count() - 1)]))
  995. ->implode('');
  996. }
  997. /**
  998. * Replace the first occurrence of the given value if it appears at the start of the string.
  999. *
  1000. * @param string $search
  1001. * @param string $replace
  1002. * @param string $subject
  1003. * @return string
  1004. */
  1005. public static function replaceStart($search, $replace, $subject)
  1006. {
  1007. $search = (string) $search;
  1008. if ($search === '') {
  1009. return $subject;
  1010. }
  1011. if (static::startsWith($subject, $search)) {
  1012. return static::replaceFirst($search, $replace, $subject);
  1013. }
  1014. return $subject;
  1015. }
  1016. /**
  1017. * Replace the last occurrence of a given value if it appears at the end of the string.
  1018. *
  1019. * @param string $search
  1020. * @param string $replace
  1021. * @param string $subject
  1022. * @return string
  1023. */
  1024. public static function replaceEnd($search, $replace, $subject)
  1025. {
  1026. $search = (string) $search;
  1027. if ($search === '') {
  1028. return $subject;
  1029. }
  1030. if (static::endsWith($subject, $search)) {
  1031. return static::replaceLast($search, $replace, $subject);
  1032. }
  1033. return $subject;
  1034. }
  1035. /**
  1036. * Replace the patterns matching the given regular expression.
  1037. *
  1038. * @param string $pattern
  1039. * @param Closure|string $replace
  1040. * @param array|string $subject
  1041. * @param int $limit
  1042. * @return null|string|string[]
  1043. */
  1044. public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
  1045. {
  1046. if ($replace instanceof Closure) {
  1047. return preg_replace_callback($pattern, $replace, $subject, $limit);
  1048. }
  1049. return preg_replace($pattern, $replace, $subject, $limit);
  1050. }
  1051. /**
  1052. * @param string $value
  1053. */
  1054. public static function reverse($value): string
  1055. {
  1056. return implode(array_reverse(mb_str_split($value)));
  1057. }
  1058. /**
  1059. * Remove all whitespace from both ends of a string.
  1060. *
  1061. * @param string $value
  1062. * @param null|string $charlist
  1063. * @return string
  1064. */
  1065. public static function trim($value, $charlist = null)
  1066. {
  1067. if ($charlist === null) {
  1068. $trimDefaultCharacters = " \n\r\t\v";
  1069. return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}' . $trimDefaultCharacters . ']+|[\s\x{FEFF}\x{200B}\x{200E}' . $trimDefaultCharacters . ']+$~u', '', $value) ?? trim($value);
  1070. }
  1071. return trim($value, $charlist);
  1072. }
  1073. /**
  1074. * Remove all whitespace from the beginning of a string.
  1075. *
  1076. * @param string $value
  1077. * @param null|string $charlist
  1078. * @return string
  1079. */
  1080. public static function ltrim($value, $charlist = null)
  1081. {
  1082. if ($charlist === null) {
  1083. $ltrimDefaultCharacters = " \n\r\t\v";
  1084. return preg_replace('~^[\s\x{FEFF}\x{200B}\x{200E}' . $ltrimDefaultCharacters . ']+~u', '', $value) ?? ltrim($value);
  1085. }
  1086. return ltrim($value, $charlist);
  1087. }
  1088. /**
  1089. * Remove all whitespace from the end of a string.
  1090. *
  1091. * @param string $value
  1092. * @param null|string $charlist
  1093. * @return string
  1094. */
  1095. public static function rtrim($value, $charlist = null)
  1096. {
  1097. if ($charlist === null) {
  1098. $rtrimDefaultCharacters = " \n\r\t\v";
  1099. return preg_replace('~[\s\x{FEFF}\x{200B}\x{200E}' . $rtrimDefaultCharacters . ']+$~u', '', $value) ?? rtrim($value);
  1100. }
  1101. return rtrim($value, $charlist);
  1102. }
  1103. /**
  1104. * Remove all "extra" blank space from the given string.
  1105. *
  1106. * @param string $value
  1107. */
  1108. public static function squish($value): null|array|string
  1109. {
  1110. return preg_replace('~(\s|\x{3164}|\x{1160})+~u', ' ', static::trim($value));
  1111. }
  1112. /**
  1113. * Replace text within a portion of a string.
  1114. *
  1115. * @param string|string[] $string
  1116. * @param string|string[] $replace
  1117. * @param int|int[] $offset
  1118. * @param null|int|int[] $length
  1119. * @return string|string[]
  1120. */
  1121. public static function substrReplace($string, $replace, $offset = 0, $length = null): array|string
  1122. {
  1123. if ($length === null) {
  1124. $length = strlen($string);
  1125. }
  1126. return substr_replace($string, $replace, $offset, $length);
  1127. }
  1128. /**
  1129. * Swap multiple keywords in a string with other keywords.
  1130. *
  1131. * @param string $subject
  1132. * @return string
  1133. */
  1134. public static function swap(array $map, $subject): array|string
  1135. {
  1136. return str_replace(array_keys($map), array_values($map), $subject);
  1137. }
  1138. /**
  1139. * Take the first or last {$limit} characters of a string.
  1140. */
  1141. public static function take(string $string, int $limit): string
  1142. {
  1143. if ($limit < 0) {
  1144. return static::substr($string, $limit);
  1145. }
  1146. return static::substr($string, 0, $limit);
  1147. }
  1148. /**
  1149. * Convert the given string to Base64 encoding.
  1150. *
  1151. * @param string $string
  1152. */
  1153. public static function toBase64($string): string
  1154. {
  1155. return base64_encode($string);
  1156. }
  1157. /**
  1158. * Split a string into pieces by uppercase characters.
  1159. *
  1160. * @param string $string
  1161. * @return bool|string[]
  1162. */
  1163. public static function ucsplit($string): array|bool
  1164. {
  1165. return preg_split('/(?=\p{Lu})/u', $string, -1, PREG_SPLIT_NO_EMPTY);
  1166. }
  1167. /**
  1168. * Unwrap the string with the given strings.
  1169. *
  1170. * @param string $value
  1171. * @param string $before
  1172. * @param null|string $after
  1173. */
  1174. public static function unwrap($value, $before, $after = null): string
  1175. {
  1176. if (static::startsWith($value, $before)) {
  1177. $value = static::substr($value, static::length($before));
  1178. }
  1179. if (static::endsWith($value, $after ??= $before)) {
  1180. $value = static::substr($value, 0, -static::length($after));
  1181. }
  1182. return $value;
  1183. }
  1184. /**
  1185. * Get the number of words a string contains.
  1186. *
  1187. * @param string $string
  1188. */
  1189. public static function wordCount($string): array|int
  1190. {
  1191. return str_word_count($string);
  1192. }
  1193. /**
  1194. * Wrap the string with the given strings.
  1195. *
  1196. * @param string $value
  1197. * @param string $before
  1198. * @param null|string $after
  1199. */
  1200. public static function wrap($value, $before, $after = null): string
  1201. {
  1202. return $before . $value . ($after ??= $before);
  1203. }
  1204. /**
  1205. * Wrap a string to a given number of characters.
  1206. *
  1207. * @param string $string
  1208. * @param int $characters
  1209. * @param string $break
  1210. * @param bool $cutLongWords
  1211. */
  1212. public static function wordWrap($string, $characters = 75, $break = "\n", $cutLongWords = false): string
  1213. {
  1214. return wordwrap($string, $characters, $break, $cutLongWords);
  1215. }
  1216. /**
  1217. * Remove all non-numeric characters from a string.
  1218. */
  1219. public static function numbers(array|string $value): array|string
  1220. {
  1221. return preg_replace('/[^0-9]/', '', $value);
  1222. }
  1223. /**
  1224. * Decode the given Base64 encoded string.
  1225. */
  1226. public static function fromBase64(string $string, bool $strict = false): false|string
  1227. {
  1228. return base64_decode($string, $strict);
  1229. }
  1230. /**
  1231. * Returns the replacements for the ascii method.
  1232. * Note: Adapted from Stringy\Stringy.
  1233. *
  1234. * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
  1235. */
  1236. protected static function charsArray(): array
  1237. {
  1238. static $charsArray;
  1239. if (isset($charsArray)) {
  1240. return $charsArray;
  1241. }
  1242. return $charsArray = [
  1243. '0' => ['°', '₀', '۰', '0'],
  1244. '1' => ['¹', '₁', '۱', '1'],
  1245. '2' => ['²', '₂', '۲', '2'],
  1246. '3' => ['³', '₃', '۳', '3'],
  1247. '4' => ['⁴', '₄', '۴', '٤', '4'],
  1248. '5' => ['⁵', '₅', '۵', '٥', '5'],
  1249. '6' => ['⁶', '₆', '۶', '٦', '6'],
  1250. '7' => ['⁷', '₇', '۷', '7'],
  1251. '8' => ['⁸', '₈', '۸', '8'],
  1252. '9' => ['⁹', '₉', '۹', '9'],
  1253. 'a' => [
  1254. 'à',
  1255. 'á',
  1256. 'ả',
  1257. 'ã',
  1258. 'ạ',
  1259. 'ă',
  1260. 'ắ',
  1261. 'ằ',
  1262. 'ẳ',
  1263. 'ẵ',
  1264. 'ặ',
  1265. 'â',
  1266. 'ấ',
  1267. 'ầ',
  1268. 'ẩ',
  1269. 'ẫ',
  1270. 'ậ',
  1271. 'ā',
  1272. 'ą',
  1273. 'å',
  1274. 'α',
  1275. 'ά',
  1276. 'ἀ',
  1277. 'ἁ',
  1278. 'ἂ',
  1279. 'ἃ',
  1280. 'ἄ',
  1281. 'ἅ',
  1282. 'ἆ',
  1283. 'ἇ',
  1284. 'ᾀ',
  1285. 'ᾁ',
  1286. 'ᾂ',
  1287. 'ᾃ',
  1288. 'ᾄ',
  1289. 'ᾅ',
  1290. 'ᾆ',
  1291. 'ᾇ',
  1292. 'ὰ',
  1293. 'ά',
  1294. 'ᾰ',
  1295. 'ᾱ',
  1296. 'ᾲ',
  1297. 'ᾳ',
  1298. 'ᾴ',
  1299. 'ᾶ',
  1300. 'ᾷ',
  1301. 'а',
  1302. 'أ',
  1303. 'အ',
  1304. 'ာ',
  1305. 'ါ',
  1306. 'ǻ',
  1307. 'ǎ',
  1308. 'ª',
  1309. 'ა',
  1310. 'अ',
  1311. 'ا',
  1312. 'a',
  1313. 'ä',
  1314. ],
  1315. 'b' => ['б', 'β', 'ب', 'ဗ', 'ბ', 'b'],
  1316. 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ', 'c'],
  1317. 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض', 'ဍ', 'ဒ', 'დ', 'd'],
  1318. 'e' => [
  1319. 'é',
  1320. 'è',
  1321. 'ẻ',
  1322. 'ẽ',
  1323. 'ẹ',
  1324. 'ê',
  1325. 'ế',
  1326. 'ề',
  1327. 'ể',
  1328. 'ễ',
  1329. 'ệ',
  1330. 'ë',
  1331. 'ē',
  1332. 'ę',
  1333. 'ě',
  1334. 'ĕ',
  1335. 'ė',
  1336. 'ε',
  1337. 'έ',
  1338. 'ἐ',
  1339. 'ἑ',
  1340. 'ἒ',
  1341. 'ἓ',
  1342. 'ἔ',
  1343. 'ἕ',
  1344. 'ὲ',
  1345. 'έ',
  1346. 'е',
  1347. 'ё',
  1348. 'э',
  1349. 'є',
  1350. 'ə',
  1351. 'ဧ',
  1352. 'ေ',
  1353. 'ဲ',
  1354. 'ე',
  1355. 'ए',
  1356. 'إ',
  1357. 'ئ',
  1358. 'e',
  1359. ],
  1360. 'f' => ['ф', 'φ', 'ف', 'ƒ', 'ფ', 'f'],
  1361. 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ', 'g'],
  1362. 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ', 'h'],
  1363. 'i' => [
  1364. 'í',
  1365. 'ì',
  1366. 'ỉ',
  1367. 'ĩ',
  1368. 'ị',
  1369. 'î',
  1370. 'ï',
  1371. 'ī',
  1372. 'ĭ',
  1373. 'į',
  1374. 'ı',
  1375. 'ι',
  1376. 'ί',
  1377. 'ϊ',
  1378. 'ΐ',
  1379. 'ἰ',
  1380. 'ἱ',
  1381. 'ἲ',
  1382. 'ἳ',
  1383. 'ἴ',
  1384. 'ἵ',
  1385. 'ἶ',
  1386. 'ἷ',
  1387. 'ὶ',
  1388. 'ί',
  1389. 'ῐ',
  1390. 'ῑ',
  1391. 'ῒ',
  1392. 'ΐ',
  1393. 'ῖ',
  1394. 'ῗ',
  1395. 'і',
  1396. 'ї',
  1397. 'и',
  1398. 'ဣ',
  1399. 'ိ',
  1400. 'ီ',
  1401. 'ည်',
  1402. 'ǐ',
  1403. 'ი',
  1404. 'इ',
  1405. 'ی',
  1406. 'i',
  1407. ],
  1408. 'j' => ['ĵ', 'ј', 'Ј', 'ჯ', 'ج', 'j'],
  1409. 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ', 'ک', 'k'],
  1410. 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ', 'l'],
  1411. 'm' => ['м', 'μ', 'م', 'မ', 'მ', 'm'],
  1412. 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န', 'ნ', 'n'],
  1413. 'o' => [
  1414. 'ó',
  1415. 'ò',
  1416. 'ỏ',
  1417. 'õ',
  1418. 'ọ',
  1419. 'ô',
  1420. 'ố',
  1421. 'ồ',
  1422. 'ổ',
  1423. 'ỗ',
  1424. 'ộ',
  1425. 'ơ',
  1426. 'ớ',
  1427. 'ờ',
  1428. 'ở',
  1429. 'ỡ',
  1430. 'ợ',
  1431. 'ø',
  1432. 'ō',
  1433. 'ő',
  1434. 'ŏ',
  1435. 'ο',
  1436. 'ὀ',
  1437. 'ὁ',
  1438. 'ὂ',
  1439. 'ὃ',
  1440. 'ὄ',
  1441. 'ὅ',
  1442. 'ὸ',
  1443. 'ό',
  1444. 'о',
  1445. 'و',
  1446. 'θ',
  1447. 'ို',
  1448. 'ǒ',
  1449. 'ǿ',
  1450. 'º',
  1451. 'ო',
  1452. 'ओ',
  1453. 'o',
  1454. 'ö',
  1455. ],
  1456. 'p' => ['п', 'π', 'ပ', 'პ', 'پ', 'p'],
  1457. 'q' => ['ყ', 'q'],
  1458. 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ', 'r'],
  1459. 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ', 'ſ', 'ს', 's'],
  1460. 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط', 'ဋ', 'တ', 'ŧ', 'თ', 'ტ', 't'],
  1461. 'u' => [
  1462. 'ú',
  1463. 'ù',
  1464. 'ủ',
  1465. 'ũ',
  1466. 'ụ',
  1467. 'ư',
  1468. 'ứ',
  1469. 'ừ',
  1470. 'ử',
  1471. 'ữ',
  1472. 'ự',
  1473. 'û',
  1474. 'ū',
  1475. 'ů',
  1476. 'ű',
  1477. 'ŭ',
  1478. 'ų',
  1479. 'µ',
  1480. 'у',
  1481. 'ဉ',
  1482. 'ု',
  1483. 'ူ',
  1484. 'ǔ',
  1485. 'ǖ',
  1486. 'ǘ',
  1487. 'ǚ',
  1488. 'ǜ',
  1489. 'უ',
  1490. 'उ',
  1491. 'u',
  1492. 'ў',
  1493. 'ü',
  1494. ],
  1495. 'v' => ['в', 'ვ', 'ϐ', 'v'],
  1496. 'w' => ['ŵ', 'ω', 'ώ', 'ဝ', 'ွ', 'w'],
  1497. 'x' => ['χ', 'ξ', 'x'],
  1498. 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي', 'ယ', 'y'],
  1499. 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ', 'z'],
  1500. 'aa' => ['ع', 'आ', 'آ'],
  1501. 'ae' => ['æ', 'ǽ'],
  1502. 'ai' => ['ऐ'],
  1503. 'ch' => ['ч', 'ჩ', 'ჭ', 'چ'],
  1504. 'dj' => ['ђ', 'đ'],
  1505. 'dz' => ['џ', 'ძ'],
  1506. 'ei' => ['ऍ'],
  1507. 'gh' => ['غ', 'ღ'],
  1508. 'ii' => ['ई'],
  1509. 'ij' => ['ij'],
  1510. 'kh' => ['х', 'خ', 'ხ'],
  1511. 'lj' => ['љ'],
  1512. 'nj' => ['њ'],
  1513. 'oe' => ['ö', 'œ', 'ؤ'],
  1514. 'oi' => ['ऑ'],
  1515. 'oii' => ['ऒ'],
  1516. 'ps' => ['ψ'],
  1517. 'sh' => ['ш', 'შ', 'ش'],
  1518. 'shch' => ['щ'],
  1519. 'ss' => ['ß'],
  1520. 'sx' => ['ŝ'],
  1521. 'th' => ['þ', 'ϑ', 'ث', 'ذ', 'ظ'],
  1522. 'ts' => ['ц', 'ც', 'წ'],
  1523. 'ue' => ['ü'],
  1524. 'uu' => ['ऊ'],
  1525. 'ya' => ['я'],
  1526. 'yu' => ['ю'],
  1527. 'zh' => ['ж', 'ჟ', 'ژ'],
  1528. '(c)' => ['©'],
  1529. 'A' => [
  1530. 'Á',
  1531. 'À',
  1532. 'Ả',
  1533. 'Ã',
  1534. 'Ạ',
  1535. 'Ă',
  1536. 'Ắ',
  1537. 'Ằ',
  1538. 'Ẳ',
  1539. 'Ẵ',
  1540. 'Ặ',
  1541. 'Â',
  1542. 'Ấ',
  1543. 'Ầ',
  1544. 'Ẩ',
  1545. 'Ẫ',
  1546. 'Ậ',
  1547. 'Å',
  1548. 'Ā',
  1549. 'Ą',
  1550. 'Α',
  1551. 'Ά',
  1552. 'Ἀ',
  1553. 'Ἁ',
  1554. 'Ἂ',
  1555. 'Ἃ',
  1556. 'Ἄ',
  1557. 'Ἅ',
  1558. 'Ἆ',
  1559. 'Ἇ',
  1560. 'ᾈ',
  1561. 'ᾉ',
  1562. 'ᾊ',
  1563. 'ᾋ',
  1564. 'ᾌ',
  1565. 'ᾍ',
  1566. 'ᾎ',
  1567. 'ᾏ',
  1568. 'Ᾰ',
  1569. 'Ᾱ',
  1570. 'Ὰ',
  1571. 'Ά',
  1572. 'ᾼ',
  1573. 'А',
  1574. 'Ǻ',
  1575. 'Ǎ',
  1576. 'A',
  1577. 'Ä',
  1578. ],
  1579. 'B' => ['Б', 'Β', 'ब', 'B'],
  1580. 'C' => ['Ç', 'Ć', 'Č', 'Ĉ', 'Ċ', 'C'],
  1581. 'D' => ['Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ', 'D'],
  1582. 'E' => [
  1583. 'É',
  1584. 'È',
  1585. 'Ẻ',
  1586. 'Ẽ',
  1587. 'Ẹ',
  1588. 'Ê',
  1589. 'Ế',
  1590. 'Ề',
  1591. 'Ể',
  1592. 'Ễ',
  1593. 'Ệ',
  1594. 'Ë',
  1595. 'Ē',
  1596. 'Ę',
  1597. 'Ě',
  1598. 'Ĕ',
  1599. 'Ė',
  1600. 'Ε',
  1601. 'Έ',
  1602. 'Ἐ',
  1603. 'Ἑ',
  1604. 'Ἒ',
  1605. 'Ἓ',
  1606. 'Ἔ',
  1607. 'Ἕ',
  1608. 'Έ',
  1609. 'Ὲ',
  1610. 'Е',
  1611. 'Ё',
  1612. 'Э',
  1613. 'Є',
  1614. 'Ə',
  1615. 'E',
  1616. ],
  1617. 'F' => ['Ф', 'Φ', 'F'],
  1618. 'G' => ['Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ', 'G'],
  1619. 'H' => ['Η', 'Ή', 'Ħ', 'H'],
  1620. 'I' => [
  1621. 'Í',
  1622. 'Ì',
  1623. 'Ỉ',
  1624. 'Ĩ',
  1625. 'Ị',
  1626. 'Î',
  1627. 'Ï',
  1628. 'Ī',
  1629. 'Ĭ',
  1630. 'Į',
  1631. 'İ',
  1632. 'Ι',
  1633. 'Ί',
  1634. 'Ϊ',
  1635. 'Ἰ',
  1636. 'Ἱ',
  1637. 'Ἳ',
  1638. 'Ἴ',
  1639. 'Ἵ',
  1640. 'Ἶ',
  1641. 'Ἷ',
  1642. 'Ῐ',
  1643. 'Ῑ',
  1644. 'Ὶ',
  1645. 'Ί',
  1646. 'И',
  1647. 'І',
  1648. 'Ї',
  1649. 'Ǐ',
  1650. 'ϒ',
  1651. 'I',
  1652. ],
  1653. 'J' => ['J'],
  1654. 'K' => ['К', 'Κ', 'K'],
  1655. 'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ', 'Ľ', 'Ŀ', 'ल', 'L'],
  1656. 'M' => ['М', 'Μ', 'M'],
  1657. 'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν', 'N'],
  1658. 'O' => [
  1659. 'Ó',
  1660. 'Ò',
  1661. 'Ỏ',
  1662. 'Õ',
  1663. 'Ọ',
  1664. 'Ô',
  1665. 'Ố',
  1666. 'Ồ',
  1667. 'Ổ',
  1668. 'Ỗ',
  1669. 'Ộ',
  1670. 'Ơ',
  1671. 'Ớ',
  1672. 'Ờ',
  1673. 'Ở',
  1674. 'Ỡ',
  1675. 'Ợ',
  1676. 'Ø',
  1677. 'Ō',
  1678. 'Ő',
  1679. 'Ŏ',
  1680. 'Ο',
  1681. 'Ό',
  1682. 'Ὀ',
  1683. 'Ὁ',
  1684. 'Ὂ',
  1685. 'Ὃ',
  1686. 'Ὄ',
  1687. 'Ὅ',
  1688. 'Ὸ',
  1689. 'Ό',
  1690. 'О',
  1691. 'Θ',
  1692. 'Ө',
  1693. 'Ǒ',
  1694. 'Ǿ',
  1695. 'O',
  1696. 'Ö',
  1697. ],
  1698. 'P' => ['П', 'Π', 'P'],
  1699. 'Q' => ['Q'],
  1700. 'R' => ['Ř', 'Ŕ', 'Р', 'Ρ', 'Ŗ', 'R'],
  1701. 'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ', 'S'],
  1702. 'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ', 'T'],
  1703. 'U' => [
  1704. 'Ú',
  1705. 'Ù',
  1706. 'Ủ',
  1707. 'Ũ',
  1708. 'Ụ',
  1709. 'Ư',
  1710. 'Ứ',
  1711. 'Ừ',
  1712. 'Ử',
  1713. 'Ữ',
  1714. 'Ự',
  1715. 'Û',
  1716. 'Ū',
  1717. 'Ů',
  1718. 'Ű',
  1719. 'Ŭ',
  1720. 'Ų',
  1721. 'У',
  1722. 'Ǔ',
  1723. 'Ǖ',
  1724. 'Ǘ',
  1725. 'Ǚ',
  1726. 'Ǜ',
  1727. 'U',
  1728. 'Ў',
  1729. 'Ü',
  1730. ],
  1731. 'V' => ['В', 'V'],
  1732. 'W' => ['Ω', 'Ώ', 'Ŵ', 'W'],
  1733. 'X' => ['Χ', 'Ξ', 'X'],
  1734. 'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ', 'Ŷ', 'Y'],
  1735. 'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ', 'Z'],
  1736. 'AE' => ['Æ', 'Ǽ'],
  1737. 'Ch' => ['Ч'],
  1738. 'Dj' => ['Ђ'],
  1739. 'Dz' => ['Џ'],
  1740. 'Gx' => ['Ĝ'],
  1741. 'Hx' => ['Ĥ'],
  1742. 'Ij' => ['IJ'],
  1743. 'Jx' => ['Ĵ'],
  1744. 'Kh' => ['Х'],
  1745. 'Lj' => ['Љ'],
  1746. 'Nj' => ['Њ'],
  1747. 'Oe' => ['Œ'],
  1748. 'Ps' => ['Ψ'],
  1749. 'Sh' => ['Ш'],
  1750. 'Shch' => ['Щ'],
  1751. 'Ss' => ['ẞ'],
  1752. 'Th' => ['Þ'],
  1753. 'Ts' => ['Ц'],
  1754. 'Ya' => ['Я'],
  1755. 'Yu' => ['Ю'],
  1756. 'Zh' => ['Ж'],
  1757. ' ' => [
  1758. "\xC2\xA0",
  1759. "\xE2\x80\x80",
  1760. "\xE2\x80\x81",
  1761. "\xE2\x80\x82",
  1762. "\xE2\x80\x83",
  1763. "\xE2\x80\x84",
  1764. "\xE2\x80\x85",
  1765. "\xE2\x80\x86",
  1766. "\xE2\x80\x87",
  1767. "\xE2\x80\x88",
  1768. "\xE2\x80\x89",
  1769. "\xE2\x80\x8A",
  1770. "\xE2\x80\xAF",
  1771. "\xE2\x81\x9F",
  1772. "\xE3\x80\x80",
  1773. "\xEF\xBE\xA0",
  1774. ],
  1775. ];
  1776. }
  1777. /**
  1778. * Returns the language specific replacements for the ascii method.
  1779. * Note: Adapted from Stringy\Stringy.
  1780. *
  1781. * @see https://github.com/danielstjules/Stringy/blob/3.1.0/LICENSE.txt
  1782. * @return null|array
  1783. */
  1784. protected static function languageSpecificCharsArray(string $language)
  1785. {
  1786. static $languageSpecific;
  1787. if (! isset($languageSpecific)) {
  1788. $languageSpecific = [
  1789. 'bg' => [
  1790. ['х', 'Х', 'щ', 'Щ', 'ъ', 'Ъ', 'ь', 'Ь'],
  1791. ['h', 'H', 'sht', 'SHT', 'a', 'А', 'y', 'Y'],
  1792. ],
  1793. 'de' => [
  1794. ['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'],
  1795. ['ae', 'oe', 'ue', 'AE', 'OE', 'UE'],
  1796. ],
  1797. ];
  1798. }
  1799. return $languageSpecific[$language] ?? null;
  1800. }
  1801. }