/vendor/danielstjules/stringy/src/StaticStringy.php

https://gitlab.com/techniconline/kmc · PHP · 979 lines · 306 code · 68 blank · 605 comment · 0 complexity · 10714f0adb9d92e7aa0e7fdb70f9c2af MD5 · raw file

  1. <?php
  2. namespace Stringy;
  3. class StaticStringy
  4. {
  5. /**
  6. * Returns an array consisting of the characters in the string.
  7. *
  8. * @param string $str String for which to return the chars
  9. * @param string $encoding The character encoding
  10. * @return array An array of string chars
  11. */
  12. public static function chars($str, $encoding = null)
  13. {
  14. return Stringy::create($str, $encoding)->chars();
  15. }
  16. /**
  17. * Converts the first character of the supplied string to upper case.
  18. *
  19. * @param string $str String to modify
  20. * @param string $encoding The character encoding
  21. * @return string String with the first character being upper case
  22. */
  23. public static function upperCaseFirst($str, $encoding = null)
  24. {
  25. return (string)Stringy::create($str, $encoding)->upperCaseFirst();
  26. }
  27. /**
  28. * Converts the first character of the supplied string to lower case.
  29. *
  30. * @param string $str String to modify
  31. * @param string $encoding The character encoding
  32. * @return string String with the first character being lower case
  33. */
  34. public static function lowerCaseFirst($str, $encoding = null)
  35. {
  36. return (string)Stringy::create($str, $encoding)->lowerCaseFirst();
  37. }
  38. /**
  39. * Returns a camelCase version of the string. Trims surrounding spaces,
  40. * capitalizes letters following digits, spaces, dashes and underscores,
  41. * and removes spaces, dashes, as well as underscores.
  42. *
  43. * @param string $str String to convert to camelCase
  44. * @param string $encoding The character encoding
  45. * @return string String in camelCase
  46. */
  47. public static function camelize($str, $encoding = null)
  48. {
  49. return (string)Stringy::create($str, $encoding)->camelize();
  50. }
  51. /**
  52. * Returns an UpperCamelCase version of the supplied string. It trims
  53. * surrounding spaces, capitalizes letters following digits, spaces, dashes
  54. * and underscores, and removes spaces, dashes, underscores.
  55. *
  56. * @param string $str String to convert to UpperCamelCase
  57. * @param string $encoding The character encoding
  58. * @return string String in UpperCamelCase
  59. */
  60. public static function upperCamelize($str, $encoding = null)
  61. {
  62. return (string)Stringy::create($str, $encoding)->upperCamelize();
  63. }
  64. /**
  65. * Returns a lowercase and trimmed string separated by dashes. Dashes are
  66. * inserted before uppercase characters (with the exception of the first
  67. * character of the string), and in place of spaces as well as underscores.
  68. *
  69. * @param string $str String to convert
  70. * @param string $encoding The character encoding
  71. * @return string Dasherized string
  72. */
  73. public static function dasherize($str, $encoding = null)
  74. {
  75. return (string)Stringy::create($str, $encoding)->dasherize();
  76. }
  77. /**
  78. * Returns a lowercase and trimmed string separated by underscores.
  79. * Underscores are inserted before uppercase characters (with the exception
  80. * of the first character of the string), and in place of spaces as well as
  81. * dashes.
  82. *
  83. * @param string $str String to convert
  84. * @param string $encoding The character encoding
  85. * @return string Underscored string
  86. */
  87. public static function underscored($str, $encoding = null)
  88. {
  89. return (string)Stringy::create($str, $encoding)->underscored();
  90. }
  91. /**
  92. * Returns a lowercase and trimmed string separated by the given delimiter.
  93. * Delimiters are inserted before uppercase characters (with the exception
  94. * of the first character of the string), and in place of spaces, dashes,
  95. * and underscores. Alpha delimiters are not converted to lowercase.
  96. *
  97. * @param string $str String to convert
  98. * @param string $delimiter Sequence used to separate parts of the string
  99. * @param string $encoding The character encoding
  100. * @return string String with delimiter
  101. */
  102. public static function delimit($str, $delimiter, $encoding = null)
  103. {
  104. return (string)Stringy::create($str, $encoding)->delimit($delimiter);
  105. }
  106. /**
  107. * Returns a case swapped version of the string.
  108. *
  109. * @param string $str String to swap case
  110. * @param string $encoding The character encoding
  111. * @return string String with each character's case swapped
  112. */
  113. public static function swapCase($str, $encoding = null)
  114. {
  115. return (string)Stringy::create($str, $encoding)->swapCase();
  116. }
  117. /**
  118. * Returns a trimmed string with the first letter of each word capitalized.
  119. * Ignores the case of other letters, preserving any acronyms. Also accepts
  120. * an array, $ignore, allowing you to list words not to be capitalized.
  121. *
  122. * @param string $str String to titleize
  123. * @param string $encoding The character encoding
  124. * @param array $ignore An array of words not to capitalize
  125. * @return string Titleized string
  126. */
  127. public static function titleize($str, $ignore = null, $encoding = null)
  128. {
  129. return (string)Stringy::create($str, $encoding)->titleize($ignore);
  130. }
  131. /**
  132. * Capitalizes the first word of the string, replaces underscores with
  133. * spaces, and strips '_id'.
  134. *
  135. * @param string $str String to humanize
  136. * @param string $encoding The character encoding
  137. * @return string A humanized string
  138. */
  139. public static function humanize($str, $encoding = null)
  140. {
  141. return (string)Stringy::create($str, $encoding)->humanize();
  142. }
  143. /**
  144. * Returns a string with smart quotes, ellipsis characters, and dashes from
  145. * Windows-1252 (commonly used in Word documents) replaced by their ASCII
  146. * equivalents.
  147. *
  148. * @param string $str String to remove special chars
  149. * @return string String with those characters removed
  150. */
  151. public static function tidy($str)
  152. {
  153. return (string)Stringy::create($str)->tidy();
  154. }
  155. /**
  156. * Trims the string and replaces consecutive whitespace characters with a
  157. * single space. This includes tabs and newline characters, as well as
  158. * multibyte whitespace such as the thin space and ideographic space.
  159. *
  160. * @param string $str The string to cleanup whitespace
  161. * @param string $encoding The character encoding
  162. * @return string The trimmed string with condensed whitespace
  163. */
  164. public static function collapseWhitespace($str, $encoding = null)
  165. {
  166. return (string)Stringy::create($str, $encoding)->collapseWhitespace();
  167. }
  168. /**
  169. * Returns an ASCII version of the string. A set of non-ASCII characters are
  170. * replaced with their closest ASCII counterparts, and the rest are removed
  171. * unless instructed otherwise.
  172. *
  173. * @param string $str A string with non-ASCII characters
  174. * @param bool $removeUnsupported Whether or not to remove the
  175. * unsupported characters
  176. * @return string A string containing only ASCII characters
  177. */
  178. public static function toAscii($str, $removeUnsupported = true)
  179. {
  180. return (string)Stringy::create($str)->toAscii($removeUnsupported);
  181. }
  182. /**
  183. * Pads the string to a given length with $padStr. If length is less than
  184. * or equal to the length of the string, no padding takes places. The
  185. * default string used for padding is a space, and the default type (one of
  186. * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
  187. * if $padType isn't one of those 3 values.
  188. *
  189. * @param string $str String to pad
  190. * @param int $length Desired string length after padding
  191. * @param string $padStr String used to pad, defaults to space
  192. * @param string $padType One of 'left', 'right', 'both'
  193. * @param string $encoding The character encoding
  194. * @return string The padded string
  195. * @throws \InvalidArgumentException If $padType isn't one of 'right',
  196. * 'left' or 'both'
  197. */
  198. public static function pad($str, $length, $padStr = ' ', $padType = 'right',
  199. $encoding = null)
  200. {
  201. return (string)Stringy::create($str, $encoding)
  202. ->pad($length, $padStr, $padType);
  203. }
  204. /**
  205. * Returns a new string of a given length such that the beginning of the
  206. * string is padded. Alias for pad() with a $padType of 'left'.
  207. *
  208. * @param string $str String to pad
  209. * @param int $length Desired string length after padding
  210. * @param string $padStr String used to pad, defaults to space
  211. * @param string $encoding The character encoding
  212. * @return string The padded string
  213. */
  214. public static function padLeft($str, $length, $padStr = ' ', $encoding = null)
  215. {
  216. return (string)Stringy::create($str, $encoding)
  217. ->padLeft($length, $padStr);
  218. }
  219. /**
  220. * Returns a new string of a given length such that the end of the string
  221. * is padded. Alias for pad() with a $padType of 'right'.
  222. *
  223. * @param string $str String to pad
  224. * @param int $length Desired string length after padding
  225. * @param string $padStr String used to pad, defaults to space
  226. * @param string $encoding The character encoding
  227. * @return string The padded string
  228. */
  229. public static function padRight($str, $length, $padStr = ' ', $encoding = null)
  230. {
  231. return (string)Stringy::create($str, $encoding)
  232. ->padRight($length, $padStr);
  233. }
  234. /**
  235. * Returns a new string of a given length such that both sides of the
  236. * string are padded. Alias for pad() with a $padType of 'both'.
  237. *
  238. * @param string $str String to pad
  239. * @param int $length Desired string length after padding
  240. * @param string $padStr String used to pad, defaults to space
  241. * @param string $encoding The character encoding
  242. * @return string The padded string
  243. */
  244. public static function padBoth($str, $length, $padStr = ' ', $encoding = null)
  245. {
  246. return (string)Stringy::create($str, $encoding)
  247. ->padBoth($length, $padStr);
  248. }
  249. /**
  250. * Returns true if the string begins with $substring, false otherwise. By
  251. * default, the comparison is case-sensitive, but can be made insensitive
  252. * by setting $caseSensitive to false.
  253. *
  254. * @param string $str String to check the start of
  255. * @param string $substring The substring to look for
  256. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  257. * @param string $encoding The character encoding
  258. * @return bool Whether or not $str starts with $substring
  259. */
  260. public static function startsWith($str, $substring, $caseSensitive = true,
  261. $encoding = null)
  262. {
  263. return Stringy::create($str, $encoding)
  264. ->startsWith($substring, $caseSensitive);
  265. }
  266. /**
  267. * Returns true if the string ends with $substring, false otherwise. By
  268. * default, the comparison is case-sensitive, but can be made insensitive
  269. * by setting $caseSensitive to false.
  270. *
  271. * @param string $str String to check the end of
  272. * @param string $substring The substring to look for
  273. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  274. * @param string $encoding The character encoding
  275. * @return bool Whether or not $str ends with $substring
  276. */
  277. public static function endsWith($str, $substring, $caseSensitive = true,
  278. $encoding = null)
  279. {
  280. return Stringy::create($str, $encoding)
  281. ->endsWith($substring, $caseSensitive);
  282. }
  283. /**
  284. * Converts each tab in the string to some number of spaces, as defined by
  285. * $tabLength. By default, each tab is converted to 4 consecutive spaces.
  286. *
  287. * @param string $str String to convert tabs to spaces
  288. * @param int $tabLength Number of spaces to replace each tab with
  289. * @return string String with tabs switched to spaces
  290. */
  291. public static function toSpaces($str, $tabLength = 4)
  292. {
  293. return (string)Stringy::create($str)->toSpaces($tabLength);
  294. }
  295. /**
  296. * Converts each occurrence of some consecutive number of spaces, as
  297. * defined by $tabLength, to a tab. By default, each 4 consecutive spaces
  298. * are converted to a tab.
  299. *
  300. * @param string $str String to convert spaces to tabs
  301. * @param int $tabLength Number of spaces to replace with a tab
  302. * @return string String with spaces switched to tabs
  303. */
  304. public static function toTabs($str, $tabLength = 4)
  305. {
  306. return (string)Stringy::create($str)->toTabs($tabLength);
  307. }
  308. /**
  309. * Converts all characters in the string to lowercase. An alias for PHP's
  310. * mb_strtolower().
  311. *
  312. * @param string $str String to convert case
  313. * @param string $encoding The character encoding
  314. * @return string The lowercase string
  315. */
  316. public static function toLowerCase($str, $encoding = null)
  317. {
  318. return (string)Stringy::create($str, $encoding)->toLowerCase();
  319. }
  320. /**
  321. * Converts the first character of each word in the string to uppercase.
  322. *
  323. * @param string $str String to convert case
  324. * @param string $encoding The character encoding
  325. * @return string The title-cased string
  326. */
  327. public static function toTitleCase($str, $encoding = null)
  328. {
  329. return (string)Stringy::create($str, $encoding)->toTitleCase();
  330. }
  331. /**
  332. * Converts all characters in the string to uppercase. An alias for PHP's
  333. * mb_strtoupper().
  334. *
  335. * @param string $str String to convert case
  336. * @param string $encoding The character encoding
  337. * @return string The uppercase string
  338. */
  339. public static function toUpperCase($str, $encoding = null)
  340. {
  341. return (string)Stringy::create($str, $encoding)->toUpperCase();
  342. }
  343. /**
  344. * Converts the string into an URL slug. This includes replacing non-ASCII
  345. * characters with their closest ASCII equivalents, removing remaining
  346. * non-ASCII and non-alphanumeric characters, and replacing whitespace with
  347. * $replacement. The replacement defaults to a single dash, and the string
  348. * is also converted to lowercase.
  349. *
  350. * @param string $str Text to transform into an URL slug
  351. * @param string $replacement The string used to replace whitespace
  352. * @return string The corresponding URL slug
  353. */
  354. public static function slugify($str, $replacement = '-')
  355. {
  356. return (string)Stringy::create($str)->slugify($replacement);
  357. }
  358. /**
  359. * Returns true if the string contains $needle, false otherwise. By default,
  360. * the comparison is case-sensitive, but can be made insensitive by setting
  361. * $caseSensitive to false.
  362. *
  363. * @param string $haystack String being checked
  364. * @param string $needle Substring to look for
  365. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  366. * @param string $encoding The character encoding
  367. * @return bool Whether or not $haystack contains $needle
  368. */
  369. public static function contains($haystack, $needle, $caseSensitive = true,
  370. $encoding = null)
  371. {
  372. return Stringy::create($haystack, $encoding)
  373. ->contains($needle, $caseSensitive);
  374. }
  375. /**
  376. * Returns true if the string contains any $needles, false otherwise. By
  377. * default, the comparison is case-sensitive, but can be made insensitive
  378. * by setting $caseSensitive to false.
  379. *
  380. * @param string $haystack String being checked
  381. * @param array $needles Substrings to look for
  382. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  383. * @param string $encoding The character encoding
  384. * @return bool Whether or not $haystack contains any $needles
  385. */
  386. public static function containsAny($haystack, $needles,
  387. $caseSensitive = true, $encoding = null)
  388. {
  389. return Stringy::create($haystack, $encoding)
  390. ->containsAny($needles, $caseSensitive);
  391. }
  392. /**
  393. * Returns true if the string contains all $needles, false otherwise. By
  394. * default, the comparison is case-sensitive, but can be made insensitive
  395. * by setting $caseSensitive to false.
  396. *
  397. * @param string $haystack String being checked
  398. * @param array $needles Substrings to look for
  399. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  400. * @param string $encoding The character encoding
  401. * @return bool Whether or not $haystack contains all $needles
  402. */
  403. public static function containsAll($haystack, $needles,
  404. $caseSensitive = true, $encoding = null)
  405. {
  406. return Stringy::create($haystack, $encoding)
  407. ->containsAll($needles, $caseSensitive);
  408. }
  409. /**
  410. * Returns the index of the first occurrence of $needle in the string,
  411. * and false if not found. Accepts an optional offset from which to begin
  412. * the search.
  413. *
  414. * @param string $haystack String to search
  415. * @param string $needle Substring to look for
  416. * @param int $offset Offset from which to search
  417. * @return int|bool The occurrence's index if found, otherwise false
  418. */
  419. public static function indexOf($haystack, $needle, $offset = 0,
  420. $encoding = null)
  421. {
  422. return Stringy::create($haystack, $encoding)
  423. ->indexOf($needle, $offset);
  424. }
  425. /**
  426. * Returns the index of the last occurrence of $needle in the string,
  427. * and false if not found. Accepts an optional offset from which to begin
  428. * the search.
  429. *
  430. * @param string $haystack String to search
  431. * @param string $needle Substring to look for
  432. * @param int $offset Offset from which to search
  433. * @return int|bool The last occurrence's index if found, otherwise false
  434. */
  435. public static function indexOfLast($haystack, $needle, $offset = 0,
  436. $encoding = null)
  437. {
  438. return Stringy::create($haystack, $encoding)
  439. ->indexOfLast($needle, $offset);
  440. }
  441. /**
  442. * Surrounds a string with the given substring.
  443. *
  444. * @param string $str The string to surround
  445. * @param string $substring The substring to add to both sides
  446. * @return string The string with the substring prepended and appended
  447. */
  448. public static function surround($str, $substring)
  449. {
  450. return (string)Stringy::create($str)->surround($substring);
  451. }
  452. /**
  453. * Inserts $substring into the string at the $index provided.
  454. *
  455. * @param string $str String to insert into
  456. * @param string $substring String to be inserted
  457. * @param int $index The index at which to insert the substring
  458. * @param string $encoding The character encoding
  459. * @return string The resulting string after the insertion
  460. */
  461. public static function insert($str, $substring, $index, $encoding = null)
  462. {
  463. return (string)Stringy::create($str, $encoding)
  464. ->insert($substring, $index);
  465. }
  466. /**
  467. * Truncates the string to a given length. If $substring is provided, and
  468. * truncating occurs, the string is further truncated so that the substring
  469. * may be appended without exceeding the desired length.
  470. *
  471. * @param string $str String to truncate
  472. * @param int $length Desired length of the truncated string
  473. * @param string $substring The substring to append if it can fit
  474. * @param string $encoding The character encoding
  475. * @return string The resulting string after truncating
  476. */
  477. public static function truncate($str, $length, $substring = '',
  478. $encoding = null)
  479. {
  480. return (string)Stringy::create($str, $encoding)
  481. ->truncate($length, $substring);
  482. }
  483. /**
  484. * Truncates the string to a given length, while ensuring that it does not
  485. * split words. If $substring is provided, and truncating occurs, the
  486. * string is further truncated so that the substring may be appended without
  487. * exceeding the desired length.
  488. *
  489. * @param string $str String to truncate
  490. * @param int $length Desired length of the truncated string
  491. * @param string $substring The substring to append if it can fit
  492. * @param string $encoding The character encoding
  493. * @return string The resulting string after truncating
  494. */
  495. public static function safeTruncate($str, $length, $substring = '',
  496. $encoding = null)
  497. {
  498. return (string)Stringy::create($str, $encoding)
  499. ->safeTruncate($length, $substring);
  500. }
  501. /**
  502. * Returns a reversed string. A multibyte version of strrev().
  503. *
  504. * @param string $str String to reverse
  505. * @param string $encoding The character encoding
  506. * @return string The reversed string
  507. */
  508. public static function reverse($str, $encoding = null)
  509. {
  510. return (string)Stringy::create($str, $encoding)->reverse();
  511. }
  512. /**
  513. * A multibyte str_shuffle() function. It returns a string with its
  514. * characters in random order.
  515. *
  516. * @param string $str String to shuffle
  517. * @param string $encoding The character encoding
  518. * @return string The shuffled string
  519. */
  520. public static function shuffle($str, $encoding = null)
  521. {
  522. return (string)Stringy::create($str, $encoding)->shuffle();
  523. }
  524. /**
  525. * Returns a string with whitespace removed from the start and end of the
  526. * string. Supports the removal of unicode whitespace. Accepts an optional
  527. * string of characters to strip instead of the defaults.
  528. *
  529. * @param string $str String to trim
  530. * @param string $chars Optional string of characters to strip
  531. * @param string $encoding The character encoding
  532. * @return string Trimmed $str
  533. */
  534. public static function trim($str, $chars = null, $encoding = null)
  535. {
  536. return (string)Stringy::create($str, $encoding)->trim($chars);
  537. }
  538. /**
  539. * Returns a string with whitespace removed from the start of the string.
  540. * Supports the removal of unicode whitespace. Accepts an optional
  541. * string of characters to strip instead of the defaults.
  542. *
  543. * @param string $str String to trim
  544. * @param string $chars Optional string of characters to strip
  545. * @param string $encoding The character encoding
  546. * @return string Trimmed $str
  547. */
  548. public static function trimLeft($str, $chars = null, $encoding = null)
  549. {
  550. return (string)Stringy::create($str, $encoding)->trimLeft($chars);
  551. }
  552. /**
  553. * Returns a string with whitespace removed from the end of the string.
  554. * Supports the removal of unicode whitespace. Accepts an optional
  555. * string of characters to strip instead of the defaults.
  556. *
  557. * @param string $str String to trim
  558. * @param string $chars Optional string of characters to strip
  559. * @param string $encoding The character encoding
  560. * @return string Trimmed $str
  561. */
  562. public static function trimRight($str, $chars = null, $encoding = null)
  563. {
  564. return (string)Stringy::create($str, $encoding)->trimRight($chars);
  565. }
  566. /**
  567. * Returns the longest common prefix between the string and $otherStr.
  568. *
  569. * @param string $str First string for comparison
  570. * @param string $otherStr Second string for comparison
  571. * @param string $encoding The character encoding
  572. * @return string The longest common prefix
  573. */
  574. public static function longestCommonPrefix($str, $otherStr, $encoding = null)
  575. {
  576. return (string)Stringy::create($str, $encoding)
  577. ->longestCommonPrefix($otherStr);
  578. }
  579. /**
  580. * Returns the longest common suffix between the string and $otherStr.
  581. *
  582. * @param string $str First string for comparison
  583. * @param string $otherStr Second string for comparison
  584. * @param string $encoding The character encoding
  585. * @return string The longest common suffix
  586. */
  587. public static function longestCommonSuffix($str, $otherStr, $encoding = null)
  588. {
  589. return (string)Stringy::create($str, $encoding)
  590. ->longestCommonSuffix($otherStr);
  591. }
  592. /**
  593. * Returns the longest common substring between the string and $otherStr.
  594. * In the case of ties, it returns that which occurs first.
  595. *
  596. * @param string $str First string for comparison
  597. * @param string $otherStr Second string for comparison
  598. * @param string $encoding The character encoding
  599. * @return string The longest common substring
  600. */
  601. public static function longestCommonSubstring($str, $otherStr,
  602. $encoding = null)
  603. {
  604. return (string)Stringy::create($str, $encoding)
  605. ->longestCommonSubstring($otherStr);
  606. }
  607. /**
  608. * Returns the length of the string. An alias for PHP's mb_strlen() function.
  609. *
  610. * @param string $str The string to get the length of
  611. * @param string $encoding The character encoding
  612. * @return int The number of characters in $str given the encoding
  613. */
  614. public static function length($str, $encoding = null)
  615. {
  616. return Stringy::create($str, $encoding)->length();
  617. }
  618. /**
  619. * Returns the substring beginning at $start with the specified $length.
  620. * It differs from the mb_substr() function in that providing a $length of
  621. * null will return the rest of the string, rather than an empty string.
  622. *
  623. * @param string $str The string to get the length of
  624. * @param int $start Position of the first character to use
  625. * @param int $length Maximum number of characters used
  626. * @param string $encoding The character encoding
  627. * @return string The substring of $str
  628. */
  629. public static function substr($str, $start, $length = null, $encoding = null)
  630. {
  631. return (string)Stringy::create($str, $encoding)->substr($start, $length);
  632. }
  633. /**
  634. * Returns the character at $index, with indexes starting at 0.
  635. *
  636. * @param string $str The string from which to get the char
  637. * @param int $index Position of the character
  638. * @param string $encoding The character encoding
  639. * @return string The character at $index
  640. */
  641. public static function at($str, $index, $encoding = null)
  642. {
  643. return (string)Stringy::create($str, $encoding)->at($index);
  644. }
  645. /**
  646. * Returns the first $n characters of the string.
  647. *
  648. * @param string $str The string from which to get the substring
  649. * @param int $n Number of chars to retrieve from the start
  650. * @param string $encoding The character encoding
  651. * @return string The first $n characters
  652. */
  653. public static function first($str, $n, $encoding = null)
  654. {
  655. return (string)Stringy::create($str, $encoding)->first($n);
  656. }
  657. /**
  658. * Returns the last $n characters of the string.
  659. *
  660. * @param string $str The string from which to get the substring
  661. * @param int $n Number of chars to retrieve from the end
  662. * @param string $encoding The character encoding
  663. * @return string The last $n characters
  664. */
  665. public static function last($str, $n, $encoding = null)
  666. {
  667. return (string)Stringy::create($str, $encoding)->last($n);
  668. }
  669. /**
  670. * Ensures that the string begins with $substring. If it doesn't, it's
  671. * prepended.
  672. *
  673. * @param string $str The string to modify
  674. * @param string $substring The substring to add if not present
  675. * @param string $encoding The character encoding
  676. * @return string The string prefixed by the $substring
  677. */
  678. public static function ensureLeft($str, $substring, $encoding = null)
  679. {
  680. return (string)Stringy::create($str, $encoding)->ensureLeft($substring);
  681. }
  682. /**
  683. * Ensures that the string begins with $substring. If it doesn't, it's
  684. * appended.
  685. *
  686. * @param string $str The string to modify
  687. * @param string $substring The substring to add if not present
  688. * @param string $encoding The character encoding
  689. * @return string The string suffixed by the $substring
  690. */
  691. public static function ensureRight($str, $substring, $encoding = null)
  692. {
  693. return (string)Stringy::create($str, $encoding)->ensureRight($substring);
  694. }
  695. /**
  696. * Returns a new string with the prefix $substring removed, if present.
  697. *
  698. * @param string $str String from which to remove the prefix
  699. * @param string $substring The prefix to remove
  700. * @param string $encoding The character encoding
  701. * @return string The string without the prefix $substring
  702. */
  703. public static function removeLeft($str, $substring, $encoding = null)
  704. {
  705. return (string)Stringy::create($str, $encoding)->removeLeft($substring);
  706. }
  707. /**
  708. * Returns a new string with the suffix $substring removed, if present.
  709. *
  710. * @param string $str String from which to remove the suffix
  711. * @param string $substring The suffix to remove
  712. * @param string $encoding The character encoding
  713. * @return string The string without the suffix $substring
  714. */
  715. public static function removeRight($str, $substring, $encoding = null)
  716. {
  717. return (string)Stringy::create($str, $encoding)->removeRight($substring);
  718. }
  719. /**
  720. * Returns true if the string contains a lower case char, false
  721. * otherwise.
  722. *
  723. * @param string $str String to check
  724. * @param string $encoding The character encoding
  725. * @return bool Whether or not $str contains a lower case character.
  726. */
  727. public static function hasLowerCase($str, $encoding = null)
  728. {
  729. return Stringy::create($str, $encoding)->hasLowerCase();
  730. }
  731. /**
  732. * Returns true if the string contains an upper case char, false
  733. * otherwise.
  734. *
  735. * @param string $str String to check
  736. * @param string $encoding The character encoding
  737. * @return bool Whether or not $str contains an upper case character.
  738. */
  739. public static function hasUpperCase($str, $encoding = null)
  740. {
  741. return Stringy::create($str, $encoding)->hasUpperCase();
  742. }
  743. /**
  744. * Returns true if the string contains only alphabetic chars, false
  745. * otherwise.
  746. *
  747. * @param string $str String to check
  748. * @param string $encoding The character encoding
  749. * @return bool Whether or not $str contains only alphabetic chars
  750. */
  751. public static function isAlpha($str, $encoding = null)
  752. {
  753. return Stringy::create($str, $encoding)->isAlpha();
  754. }
  755. /**
  756. * Returns true if the string contains only alphabetic and numeric chars,
  757. * false otherwise.
  758. *
  759. * @param string $str String to check
  760. * @param string $encoding The character encoding
  761. * @return bool Whether or not $str contains only alphanumeric chars
  762. */
  763. public static function isAlphanumeric($str, $encoding = null)
  764. {
  765. return Stringy::create($str, $encoding)->isAlphanumeric();
  766. }
  767. /**
  768. * Returns true if the string contains only whitespace chars, false
  769. * otherwise.
  770. *
  771. * @param string $str String to check
  772. * @param string $encoding The character encoding
  773. * @return bool Whether or not $str contains only whitespace characters
  774. */
  775. public static function isBlank($str, $encoding = null)
  776. {
  777. return Stringy::create($str, $encoding)->isBlank();
  778. }
  779. /**
  780. * Returns true if the string is JSON, false otherwise.
  781. *
  782. * @param string $str String to check
  783. * @param string $encoding The character encoding
  784. * @return bool Whether or not $str is JSON
  785. */
  786. public static function isJson($str, $encoding = null)
  787. {
  788. return Stringy::create($str, $encoding)->isJson();
  789. }
  790. /**
  791. * Returns true if the string contains only lower case chars, false
  792. * otherwise.
  793. *
  794. * @param string $str String to check
  795. * @param string $encoding The character encoding
  796. * @return bool Whether or not $str contains only lower case characters
  797. */
  798. public static function isLowerCase($str, $encoding = null)
  799. {
  800. return Stringy::create($str, $encoding)->isLowerCase();
  801. }
  802. /**
  803. * Returns true if the string is serialized, false otherwise.
  804. *
  805. * @param string $str String to check
  806. * @param string $encoding The character encoding
  807. * @return bool Whether or not $str is serialized
  808. */
  809. public static function isSerialized($str, $encoding = null)
  810. {
  811. return Stringy::create($str, $encoding)->isSerialized();
  812. }
  813. /**
  814. * Returns true if the string contains only upper case chars, false
  815. * otherwise.
  816. *
  817. * @param string $str String to check
  818. * @param string $encoding The character encoding
  819. * @return bool Whether or not $str contains only upper case characters
  820. */
  821. public static function isUpperCase($str, $encoding = null)
  822. {
  823. return Stringy::create($str, $encoding)->isUpperCase();
  824. }
  825. /**
  826. * Returns true if the string contains only hexadecimal chars, false
  827. * otherwise.
  828. *
  829. * @param string $str String to check
  830. * @param string $encoding The character encoding
  831. * @return bool Whether or not $str contains only hexadecimal characters
  832. */
  833. public static function isHexadecimal($str, $encoding = null)
  834. {
  835. return Stringy::create($str, $encoding)->isHexadecimal();
  836. }
  837. /**
  838. * Returns the number of occurrences of $substring in the given string.
  839. * By default, the comparison is case-sensitive, but can be made insensitive
  840. * by setting $caseSensitive to false.
  841. *
  842. * @param string $str The string to search through
  843. * @param string $substring The substring to search for
  844. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  845. * @param string $encoding The character encoding
  846. * @return int The number of $substring occurrences
  847. */
  848. public static function countSubstr($str, $substring, $caseSensitive = true,
  849. $encoding = null)
  850. {
  851. return Stringy::create($str, $encoding)
  852. ->countSubstr($substring, $caseSensitive);
  853. }
  854. /**
  855. * Replaces all occurrences of $search in $str by $replacement.
  856. *
  857. * @param string $str The haystack to search through
  858. * @param string $search The needle to search for
  859. * @param string $replacement The string to replace with
  860. * @param string $encoding The character encoding
  861. * @return string The resulting string after the replacements
  862. */
  863. public static function replace($str, $search, $replacement, $encoding = null)
  864. {
  865. return (string)Stringy::create($str, $encoding)
  866. ->replace($search, $replacement);
  867. }
  868. /**
  869. * Replaces all occurrences of $pattern in $str by $replacement. An alias
  870. * for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
  871. * in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
  872. * in the bundled version of Oniguruma in PHP 5.3.
  873. *
  874. * @param string $str The haystack to search through
  875. * @param string $pattern The regular expression pattern
  876. * @param string $replacement The string to replace with
  877. * @param string $options Matching conditions to be used
  878. * @param string $encoding The character encoding
  879. * @return string The resulting string after the replacements
  880. */
  881. public static function regexReplace($str, $pattern, $replacement,
  882. $options = 'msr', $encoding = null)
  883. {
  884. return (string)Stringy::create($str, $encoding)
  885. ->regexReplace($pattern, $replacement, $options, $encoding);
  886. }
  887. /**
  888. * Convert all applicable characters to HTML entities.
  889. *
  890. * @param string $str The string to encode.
  891. * @param int|null $flags See http://php.net/manual/en/function.htmlentities.php
  892. * @param string $encoding The character encoding
  893. * @return Stringy Object with the resulting $str after being html encoded.
  894. */
  895. public static function htmlEncode($str, $flags = ENT_COMPAT, $encoding = null)
  896. {
  897. return (string)Stringy::create($str, $encoding)->htmlEncode($flags);
  898. }
  899. /**
  900. * Convert all HTML entities to their applicable characters.
  901. *
  902. * @param string $str The string to decode.
  903. * @param int|null $flags See http://php.net/manual/en/function.html-entity-decode.php
  904. * @param string $encoding The character encoding
  905. * @return Stringy Object with the resulting $str after being html decoded.
  906. */
  907. public static function htmlDecode($str, $flags = ENT_COMPAT, $encoding = null)
  908. {
  909. return (string)Stringy::create($str, $encoding)->htmlDecode($flags);
  910. }
  911. }