Str.php 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. <?php
  2. namespace Illuminate\Support;
  3. use Illuminate\Support\Traits\Macroable;
  4. use League\CommonMark\GithubFlavoredMarkdownConverter;
  5. use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
  6. use Ramsey\Uuid\Generator\CombGenerator;
  7. use Ramsey\Uuid\Uuid;
  8. use Ramsey\Uuid\UuidFactory;
  9. use voku\helper\ASCII;
  10. class Str
  11. {
  12. use Macroable;
  13. /**
  14. * The cache of snake-cased words.
  15. *
  16. * @var array
  17. */
  18. protected static $snakeCache = [];
  19. /**
  20. * The cache of camel-cased words.
  21. *
  22. * @var array
  23. */
  24. protected static $camelCache = [];
  25. /**
  26. * The cache of studly-cased words.
  27. *
  28. * @var array
  29. */
  30. protected static $studlyCache = [];
  31. /**
  32. * The callback that should be used to generate UUIDs.
  33. *
  34. * @var callable
  35. */
  36. protected static $uuidFactory;
  37. /**
  38. * Get a new stringable object from the given string.
  39. *
  40. * @param string $string
  41. * @return \Illuminate\Support\Stringable
  42. */
  43. public static function of($string)
  44. {
  45. return new Stringable($string);
  46. }
  47. /**
  48. * Return the remainder of a string after the first occurrence of a given value.
  49. *
  50. * @param string $subject
  51. * @param string $search
  52. * @return string
  53. */
  54. public static function after($subject, $search)
  55. {
  56. return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
  57. }
  58. /**
  59. * Return the remainder of a string after the last occurrence of a given value.
  60. *
  61. * @param string $subject
  62. * @param string $search
  63. * @return string
  64. */
  65. public static function afterLast($subject, $search)
  66. {
  67. if ($search === '') {
  68. return $subject;
  69. }
  70. $position = strrpos($subject, (string) $search);
  71. if ($position === false) {
  72. return $subject;
  73. }
  74. return substr($subject, $position + strlen($search));
  75. }
  76. /**
  77. * Transliterate a UTF-8 value to ASCII.
  78. *
  79. * @param string $value
  80. * @param string $language
  81. * @return string
  82. */
  83. public static function ascii($value, $language = 'en')
  84. {
  85. return ASCII::to_ascii((string) $value, $language);
  86. }
  87. /**
  88. * Transliterate a string to its closest ASCII representation.
  89. *
  90. * @param string $string
  91. * @param string|null $unknown
  92. * @param bool|null $strict
  93. * @return string
  94. */
  95. public static function transliterate($string, $unknown = '?', $strict = false)
  96. {
  97. return ASCII::to_transliterate($string, $unknown, $strict);
  98. }
  99. /**
  100. * Get the portion of a string before the first occurrence of a given value.
  101. *
  102. * @param string $subject
  103. * @param string $search
  104. * @return string
  105. */
  106. public static function before($subject, $search)
  107. {
  108. if ($search === '') {
  109. return $subject;
  110. }
  111. $result = strstr($subject, (string) $search, true);
  112. return $result === false ? $subject : $result;
  113. }
  114. /**
  115. * Get the portion of a string before the last occurrence of a given value.
  116. *
  117. * @param string $subject
  118. * @param string $search
  119. * @return string
  120. */
  121. public static function beforeLast($subject, $search)
  122. {
  123. if ($search === '') {
  124. return $subject;
  125. }
  126. $pos = mb_strrpos($subject, $search);
  127. if ($pos === false) {
  128. return $subject;
  129. }
  130. return static::substr($subject, 0, $pos);
  131. }
  132. /**
  133. * Get the portion of a string between two given values.
  134. *
  135. * @param string $subject
  136. * @param string $from
  137. * @param string $to
  138. * @return string
  139. */
  140. public static function between($subject, $from, $to)
  141. {
  142. if ($from === '' || $to === '') {
  143. return $subject;
  144. }
  145. return static::beforeLast(static::after($subject, $from), $to);
  146. }
  147. /**
  148. * Convert a value to camel case.
  149. *
  150. * @param string $value
  151. * @return string
  152. */
  153. public static function camel($value)
  154. {
  155. if (isset(static::$camelCache[$value])) {
  156. return static::$camelCache[$value];
  157. }
  158. return static::$camelCache[$value] = lcfirst(static::studly($value));
  159. }
  160. /**
  161. * Determine if a given string contains a given substring.
  162. *
  163. * @param string $haystack
  164. * @param string|string[] $needles
  165. * @return bool
  166. */
  167. public static function contains($haystack, $needles)
  168. {
  169. foreach ((array) $needles as $needle) {
  170. if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
  171. return true;
  172. }
  173. }
  174. return false;
  175. }
  176. /**
  177. * Determine if a given string contains all array values.
  178. *
  179. * @param string $haystack
  180. * @param string[] $needles
  181. * @return bool
  182. */
  183. public static function containsAll($haystack, array $needles)
  184. {
  185. foreach ($needles as $needle) {
  186. if (! static::contains($haystack, $needle)) {
  187. return false;
  188. }
  189. }
  190. return true;
  191. }
  192. /**
  193. * Determine if a given string ends with a given substring.
  194. *
  195. * @param string $haystack
  196. * @param string|string[] $needles
  197. * @return bool
  198. */
  199. public static function endsWith($haystack, $needles)
  200. {
  201. foreach ((array) $needles as $needle) {
  202. if (
  203. $needle !== '' && $needle !== null
  204. && substr($haystack, -strlen($needle)) === (string) $needle
  205. ) {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * Cap a string with a single instance of a given value.
  213. *
  214. * @param string $value
  215. * @param string $cap
  216. * @return string
  217. */
  218. public static function finish($value, $cap)
  219. {
  220. $quoted = preg_quote($cap, '/');
  221. return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
  222. }
  223. /**
  224. * Determine if a given string matches a given pattern.
  225. *
  226. * @param string|array $pattern
  227. * @param string $value
  228. * @return bool
  229. */
  230. public static function is($pattern, $value)
  231. {
  232. $patterns = Arr::wrap($pattern);
  233. $value = (string) $value;
  234. if (empty($patterns)) {
  235. return false;
  236. }
  237. foreach ($patterns as $pattern) {
  238. $pattern = (string) $pattern;
  239. // If the given value is an exact match we can of course return true right
  240. // from the beginning. Otherwise, we will translate asterisks and do an
  241. // actual pattern match against the two strings to see if they match.
  242. if ($pattern == $value) {
  243. return true;
  244. }
  245. $pattern = preg_quote($pattern, '#');
  246. // Asterisks are translated into zero-or-more regular expression wildcards
  247. // to make it convenient to check if the strings starts with the given
  248. // pattern such as "library/*", making any string check convenient.
  249. $pattern = str_replace('\*', '.*', $pattern);
  250. if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. /**
  257. * Determine if a given string is 7 bit ASCII.
  258. *
  259. * @param string $value
  260. * @return bool
  261. */
  262. public static function isAscii($value)
  263. {
  264. return ASCII::is_ascii((string) $value);
  265. }
  266. /**
  267. * Determine if a given string is a valid UUID.
  268. *
  269. * @param string $value
  270. * @return bool
  271. */
  272. public static function isUuid($value)
  273. {
  274. if (! is_string($value)) {
  275. return false;
  276. }
  277. return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;
  278. }
  279. /**
  280. * Convert a string to kebab case.
  281. *
  282. * @param string $value
  283. * @return string
  284. */
  285. public static function kebab($value)
  286. {
  287. return static::snake($value, '-');
  288. }
  289. /**
  290. * Return the length of the given string.
  291. *
  292. * @param string $value
  293. * @param string|null $encoding
  294. * @return int
  295. */
  296. public static function length($value, $encoding = null)
  297. {
  298. if ($encoding) {
  299. return mb_strlen($value, $encoding);
  300. }
  301. return mb_strlen($value);
  302. }
  303. /**
  304. * Limit the number of characters in a string.
  305. *
  306. * @param string $value
  307. * @param int $limit
  308. * @param string $end
  309. * @return string
  310. */
  311. public static function limit($value, $limit = 100, $end = '...')
  312. {
  313. if (mb_strwidth($value, 'UTF-8') <= $limit) {
  314. return $value;
  315. }
  316. return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
  317. }
  318. /**
  319. * Convert the given string to lower-case.
  320. *
  321. * @param string $value
  322. * @return string
  323. */
  324. public static function lower($value)
  325. {
  326. return mb_strtolower($value, 'UTF-8');
  327. }
  328. /**
  329. * Limit the number of words in a string.
  330. *
  331. * @param string $value
  332. * @param int $words
  333. * @param string $end
  334. * @return string
  335. */
  336. public static function words($value, $words = 100, $end = '...')
  337. {
  338. preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
  339. if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
  340. return $value;
  341. }
  342. return rtrim($matches[0]).$end;
  343. }
  344. /**
  345. * Converts GitHub flavored Markdown into HTML.
  346. *
  347. * @param string $string
  348. * @param array $options
  349. * @return string
  350. */
  351. public static function markdown($string, array $options = [])
  352. {
  353. $converter = new GithubFlavoredMarkdownConverter($options);
  354. return (string) $converter->convertToHtml($string);
  355. }
  356. /**
  357. * Masks a portion of a string with a repeated character.
  358. *
  359. * @param string $string
  360. * @param string $character
  361. * @param int $index
  362. * @param int|null $length
  363. * @param string $encoding
  364. * @return string
  365. */
  366. public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8')
  367. {
  368. if ($character === '') {
  369. return $string;
  370. }
  371. if (is_null($length) && PHP_MAJOR_VERSION < 8) {
  372. $length = mb_strlen($string, $encoding);
  373. }
  374. $segment = mb_substr($string, $index, $length, $encoding);
  375. if ($segment === '') {
  376. return $string;
  377. }
  378. $strlen = mb_strlen($string, $encoding);
  379. $startIndex = $index;
  380. if ($index < 0) {
  381. $startIndex = $index < -$strlen ? 0 : $strlen + $index;
  382. }
  383. $start = mb_substr($string, 0, $startIndex, $encoding);
  384. $segmentLen = mb_strlen($segment, $encoding);
  385. $end = mb_substr($string, $startIndex + $segmentLen);
  386. return $start.str_repeat(mb_substr($character, 0, 1, $encoding), $segmentLen).$end;
  387. }
  388. /**
  389. * Get the string matching the given pattern.
  390. *
  391. * @param string $pattern
  392. * @param string $subject
  393. * @return string
  394. */
  395. public static function match($pattern, $subject)
  396. {
  397. preg_match($pattern, $subject, $matches);
  398. if (! $matches) {
  399. return '';
  400. }
  401. return $matches[1] ?? $matches[0];
  402. }
  403. /**
  404. * Get the string matching the given pattern.
  405. *
  406. * @param string $pattern
  407. * @param string $subject
  408. * @return \Illuminate\Support\Collection
  409. */
  410. public static function matchAll($pattern, $subject)
  411. {
  412. preg_match_all($pattern, $subject, $matches);
  413. if (empty($matches[0])) {
  414. return collect();
  415. }
  416. return collect($matches[1] ?? $matches[0]);
  417. }
  418. /**
  419. * Pad both sides of a string with another.
  420. *
  421. * @param string $value
  422. * @param int $length
  423. * @param string $pad
  424. * @return string
  425. */
  426. public static function padBoth($value, $length, $pad = ' ')
  427. {
  428. return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_BOTH);
  429. }
  430. /**
  431. * Pad the left side of a string with another.
  432. *
  433. * @param string $value
  434. * @param int $length
  435. * @param string $pad
  436. * @return string
  437. */
  438. public static function padLeft($value, $length, $pad = ' ')
  439. {
  440. return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_LEFT);
  441. }
  442. /**
  443. * Pad the right side of a string with another.
  444. *
  445. * @param string $value
  446. * @param int $length
  447. * @param string $pad
  448. * @return string
  449. */
  450. public static function padRight($value, $length, $pad = ' ')
  451. {
  452. return str_pad($value, strlen($value) - mb_strlen($value) + $length, $pad, STR_PAD_RIGHT);
  453. }
  454. /**
  455. * Parse a Class[@]method style callback into class and method.
  456. *
  457. * @param string $callback
  458. * @param string|null $default
  459. * @return array<int, string|null>
  460. */
  461. public static function parseCallback($callback, $default = null)
  462. {
  463. return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
  464. }
  465. /**
  466. * Get the plural form of an English word.
  467. *
  468. * @param string $value
  469. * @param int|array|\Countable $count
  470. * @return string
  471. */
  472. public static function plural($value, $count = 2)
  473. {
  474. return Pluralizer::plural($value, $count);
  475. }
  476. /**
  477. * Pluralize the last word of an English, studly caps case string.
  478. *
  479. * @param string $value
  480. * @param int|array|\Countable $count
  481. * @return string
  482. */
  483. public static function pluralStudly($value, $count = 2)
  484. {
  485. $parts = preg_split('/(.)(?=[A-Z])/u', $value, -1, PREG_SPLIT_DELIM_CAPTURE);
  486. $lastWord = array_pop($parts);
  487. return implode('', $parts).self::plural($lastWord, $count);
  488. }
  489. /**
  490. * Generate a more truly "random" alpha-numeric string.
  491. *
  492. * @param int $length
  493. * @return string
  494. */
  495. public static function random($length = 16)
  496. {
  497. $string = '';
  498. while (($len = strlen($string)) < $length) {
  499. $size = $length - $len;
  500. $bytes = random_bytes($size);
  501. $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
  502. }
  503. return $string;
  504. }
  505. /**
  506. * Repeat the given string.
  507. *
  508. * @param string $string
  509. * @param int $times
  510. * @return string
  511. */
  512. public static function repeat(string $string, int $times)
  513. {
  514. return str_repeat($string, $times);
  515. }
  516. /**
  517. * Replace a given value in the string sequentially with an array.
  518. *
  519. * @param string $search
  520. * @param array<int|string, string> $replace
  521. * @param string $subject
  522. * @return string
  523. */
  524. public static function replaceArray($search, array $replace, $subject)
  525. {
  526. $segments = explode($search, $subject);
  527. $result = array_shift($segments);
  528. foreach ($segments as $segment) {
  529. $result .= (array_shift($replace) ?? $search).$segment;
  530. }
  531. return $result;
  532. }
  533. /**
  534. * Replace the given value in the given string.
  535. *
  536. * @param string|string[] $search
  537. * @param string|string[] $replace
  538. * @param string|string[] $subject
  539. * @return string
  540. */
  541. public static function replace($search, $replace, $subject)
  542. {
  543. return str_replace($search, $replace, $subject);
  544. }
  545. /**
  546. * Replace the first occurrence of a given value in the string.
  547. *
  548. * @param string $search
  549. * @param string $replace
  550. * @param string $subject
  551. * @return string
  552. */
  553. public static function replaceFirst($search, $replace, $subject)
  554. {
  555. if ($search === '') {
  556. return $subject;
  557. }
  558. $position = strpos($subject, $search);
  559. if ($position !== false) {
  560. return substr_replace($subject, $replace, $position, strlen($search));
  561. }
  562. return $subject;
  563. }
  564. /**
  565. * Replace the last occurrence of a given value in the string.
  566. *
  567. * @param string $search
  568. * @param string $replace
  569. * @param string $subject
  570. * @return string
  571. */
  572. public static function replaceLast($search, $replace, $subject)
  573. {
  574. if ($search === '') {
  575. return $subject;
  576. }
  577. $position = strrpos($subject, $search);
  578. if ($position !== false) {
  579. return substr_replace($subject, $replace, $position, strlen($search));
  580. }
  581. return $subject;
  582. }
  583. /**
  584. * Remove any occurrence of the given string in the subject.
  585. *
  586. * @param string|array<string> $search
  587. * @param string $subject
  588. * @param bool $caseSensitive
  589. * @return string
  590. */
  591. public static function remove($search, $subject, $caseSensitive = true)
  592. {
  593. $subject = $caseSensitive
  594. ? str_replace($search, '', $subject)
  595. : str_ireplace($search, '', $subject);
  596. return $subject;
  597. }
  598. /**
  599. * Reverse the given string.
  600. *
  601. * @param string $value
  602. * @return string
  603. */
  604. public static function reverse(string $value)
  605. {
  606. return implode(array_reverse(mb_str_split($value)));
  607. }
  608. /**
  609. * Begin a string with a single instance of a given value.
  610. *
  611. * @param string $value
  612. * @param string $prefix
  613. * @return string
  614. */
  615. public static function start($value, $prefix)
  616. {
  617. $quoted = preg_quote($prefix, '/');
  618. return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value);
  619. }
  620. /**
  621. * Convert the given string to upper-case.
  622. *
  623. * @param string $value
  624. * @return string
  625. */
  626. public static function upper($value)
  627. {
  628. return mb_strtoupper($value, 'UTF-8');
  629. }
  630. /**
  631. * Convert the given string to title case.
  632. *
  633. * @param string $value
  634. * @return string
  635. */
  636. public static function title($value)
  637. {
  638. return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
  639. }
  640. /**
  641. * Convert the given string to title case for each word.
  642. *
  643. * @param string $value
  644. * @return string
  645. */
  646. public static function headline($value)
  647. {
  648. $parts = explode(' ', $value);
  649. $parts = count($parts) > 1
  650. ? $parts = array_map([static::class, 'title'], $parts)
  651. : $parts = array_map([static::class, 'title'], static::ucsplit(implode('_', $parts)));
  652. $collapsed = static::replace(['-', '_', ' '], '_', implode('_', $parts));
  653. return implode(' ', array_filter(explode('_', $collapsed)));
  654. }
  655. /**
  656. * Get the singular form of an English word.
  657. *
  658. * @param string $value
  659. * @return string
  660. */
  661. public static function singular($value)
  662. {
  663. return Pluralizer::singular($value);
  664. }
  665. /**
  666. * Generate a URL friendly "slug" from a given string.
  667. *
  668. * @param string $title
  669. * @param string $separator
  670. * @param string|null $language
  671. * @return string
  672. */
  673. public static function slug($title, $separator = '-', $language = 'en')
  674. {
  675. $title = $language ? static::ascii($title, $language) : $title;
  676. // Convert all dashes/underscores into separator
  677. $flip = $separator === '-' ? '_' : '-';
  678. $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
  679. // Replace @ with the word 'at'
  680. $title = str_replace('@', $separator.'at'.$separator, $title);
  681. // Remove all characters that are not the separator, letters, numbers, or whitespace.
  682. $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', static::lower($title));
  683. // Replace all separator characters and whitespace by a single separator
  684. $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
  685. return trim($title, $separator);
  686. }
  687. /**
  688. * Convert a string to snake case.
  689. *
  690. * @param string $value
  691. * @param string $delimiter
  692. * @return string
  693. */
  694. public static function snake($value, $delimiter = '_')
  695. {
  696. $key = $value;
  697. if (isset(static::$snakeCache[$key][$delimiter])) {
  698. return static::$snakeCache[$key][$delimiter];
  699. }
  700. if (! ctype_lower($value)) {
  701. $value = preg_replace('/\s+/u', '', ucwords($value));
  702. $value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
  703. }
  704. return static::$snakeCache[$key][$delimiter] = $value;
  705. }
  706. /**
  707. * Determine if a given string starts with a given substring.
  708. *
  709. * @param string $haystack
  710. * @param string|string[] $needles
  711. * @return bool
  712. */
  713. public static function startsWith($haystack, $needles)
  714. {
  715. foreach ((array) $needles as $needle) {
  716. if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
  717. return true;
  718. }
  719. }
  720. return false;
  721. }
  722. /**
  723. * Convert a value to studly caps case.
  724. *
  725. * @param string $value
  726. * @return string
  727. */
  728. public static function studly($value)
  729. {
  730. $key = $value;
  731. if (isset(static::$studlyCache[$key])) {
  732. return static::$studlyCache[$key];
  733. }
  734. $words = explode(' ', static::replace(['-', '_'], ' ', $value));
  735. $studlyWords = array_map(function ($word) {
  736. return static::ucfirst($word);
  737. }, $words);
  738. return static::$studlyCache[$key] = implode($studlyWords);
  739. }
  740. /**
  741. * Returns the portion of the string specified by the start and length parameters.
  742. *
  743. * @param string $string
  744. * @param int $start
  745. * @param int|null $length
  746. * @return string
  747. */
  748. public static function substr($string, $start, $length = null)
  749. {
  750. return mb_substr($string, $start, $length, 'UTF-8');
  751. }
  752. /**
  753. * Returns the number of substring occurrences.
  754. *
  755. * @param string $haystack
  756. * @param string $needle
  757. * @param int $offset
  758. * @param int|null $length
  759. * @return int
  760. */
  761. public static function substrCount($haystack, $needle, $offset = 0, $length = null)
  762. {
  763. if (! is_null($length)) {
  764. return substr_count($haystack, $needle, $offset, $length);
  765. } else {
  766. return substr_count($haystack, $needle, $offset);
  767. }
  768. }
  769. /**
  770. * Replace text within a portion of a string.
  771. *
  772. * @param string|array $string
  773. * @param string|array $replace
  774. * @param array|int $offset
  775. * @param array|int|null $length
  776. * @return string|array
  777. */
  778. public static function substrReplace($string, $replace, $offset = 0, $length = null)
  779. {
  780. if ($length === null) {
  781. $length = strlen($string);
  782. }
  783. return substr_replace($string, $replace, $offset, $length);
  784. }
  785. /**
  786. * Swap multiple keywords in a string with other keywords.
  787. *
  788. * @param array $map
  789. * @param string $subject
  790. * @return string
  791. */
  792. public static function swap(array $map, $subject)
  793. {
  794. return strtr($subject, $map);
  795. }
  796. /**
  797. * Make a string's first character uppercase.
  798. *
  799. * @param string $string
  800. * @return string
  801. */
  802. public static function ucfirst($string)
  803. {
  804. return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
  805. }
  806. /**
  807. * Split a string into pieces by uppercase characters.
  808. *
  809. * @param string $string
  810. * @return array
  811. */
  812. public static function ucsplit($string)
  813. {
  814. return preg_split('/(?=\p{Lu})/u', $string, -1, PREG_SPLIT_NO_EMPTY);
  815. }
  816. /**
  817. * Get the number of words a string contains.
  818. *
  819. * @param string $string
  820. * @return int
  821. */
  822. public static function wordCount($string)
  823. {
  824. return str_word_count($string);
  825. }
  826. /**
  827. * Generate a UUID (version 4).
  828. *
  829. * @return \Ramsey\Uuid\UuidInterface
  830. */
  831. public static function uuid()
  832. {
  833. return static::$uuidFactory
  834. ? call_user_func(static::$uuidFactory)
  835. : Uuid::uuid4();
  836. }
  837. /**
  838. * Generate a time-ordered UUID (version 4).
  839. *
  840. * @return \Ramsey\Uuid\UuidInterface
  841. */
  842. public static function orderedUuid()
  843. {
  844. if (static::$uuidFactory) {
  845. return call_user_func(static::$uuidFactory);
  846. }
  847. $factory = new UuidFactory;
  848. $factory->setRandomGenerator(new CombGenerator(
  849. $factory->getRandomGenerator(),
  850. $factory->getNumberConverter()
  851. ));
  852. $factory->setCodec(new TimestampFirstCombCodec(
  853. $factory->getUuidBuilder()
  854. ));
  855. return $factory->uuid4();
  856. }
  857. /**
  858. * Set the callable that will be used to generate UUIDs.
  859. *
  860. * @param callable|null $factory
  861. * @return void
  862. */
  863. public static function createUuidsUsing(callable $factory = null)
  864. {
  865. static::$uuidFactory = $factory;
  866. }
  867. /**
  868. * Indicate that UUIDs should be created normally and not using a custom factory.
  869. *
  870. * @return void
  871. */
  872. public static function createUuidsNormally()
  873. {
  874. static::$uuidFactory = null;
  875. }
  876. /**
  877. * Remove all strings from the casing caches.
  878. *
  879. * @return void
  880. */
  881. public static function flushCache()
  882. {
  883. static::$snakeCache = [];
  884. static::$camelCache = [];
  885. static::$studlyCache = [];
  886. }
  887. }