No Description

Stringy.php 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. <?php
  2. namespace Stringy;
  3. class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
  4. {
  5. /**
  6. * An instance's string.
  7. *
  8. * @var string
  9. */
  10. protected $str;
  11. /**
  12. * The string's encoding, which should be one of the mbstring module's
  13. * supported encodings.
  14. *
  15. * @var string
  16. */
  17. protected $encoding;
  18. /**
  19. * Initializes a Stringy object and assigns both str and encoding properties
  20. * the supplied values. $str is cast to a string prior to assignment, and if
  21. * $encoding is not specified, it defaults to mb_internal_encoding(). Throws
  22. * an InvalidArgumentException if the first argument is an array or object
  23. * without a __toString method.
  24. *
  25. * @param mixed $str Value to modify, after being cast to string
  26. * @param string $encoding The character encoding
  27. * @throws \InvalidArgumentException if an array or object without a
  28. * __toString method is passed as the first argument
  29. */
  30. public function __construct($str, $encoding = null)
  31. {
  32. if (is_array($str)) {
  33. throw new \InvalidArgumentException(
  34. 'Passed value cannot be an array'
  35. );
  36. } elseif (is_object($str) && !method_exists($str, '__toString')) {
  37. throw new \InvalidArgumentException(
  38. 'Passed object must have a __toString method'
  39. );
  40. }
  41. $this->str = (string) $str;
  42. $this->encoding = $encoding ?: mb_internal_encoding();
  43. }
  44. /**
  45. * Creates a Stringy object and assigns both str and encoding properties
  46. * the supplied values. $str is cast to a string prior to assignment, and if
  47. * $encoding is not specified, it defaults to mb_internal_encoding(). It
  48. * then returns the initialized object. Throws an InvalidArgumentException
  49. * if the first argument is an array or object without a __toString method.
  50. *
  51. * @param mixed $str Value to modify, after being cast to string
  52. * @param string $encoding The character encoding
  53. * @return Stringy A Stringy object
  54. * @throws \InvalidArgumentException if an array or object without a
  55. * __toString method is passed as the first argument
  56. */
  57. public static function create($str, $encoding = null)
  58. {
  59. return new static($str, $encoding);
  60. }
  61. /**
  62. * Returns the value in $str.
  63. *
  64. * @return string The current value of the $str property
  65. */
  66. public function __toString()
  67. {
  68. return $this->str;
  69. }
  70. /**
  71. * Returns the encoding used by the Stringy object.
  72. *
  73. * @return string The current value of the $encoding property
  74. */
  75. public function getEncoding()
  76. {
  77. return $this->encoding;
  78. }
  79. /**
  80. * Returns the length of the string, implementing the countable interface.
  81. *
  82. * @return int The number of characters in the string, given the encoding
  83. */
  84. public function count()
  85. {
  86. return $this->length();
  87. }
  88. /**
  89. * Returns a new ArrayIterator, thus implementing the IteratorAggregate
  90. * interface. The ArrayIterator's constructor is passed an array of chars
  91. * in the multibyte string. This enables the use of foreach with instances
  92. * of Stringy\Stringy.
  93. *
  94. * @return \ArrayIterator An iterator for the characters in the string
  95. */
  96. public function getIterator()
  97. {
  98. return new \ArrayIterator($this->chars());
  99. }
  100. /**
  101. * Returns whether or not a character exists at an index. Offsets may be
  102. * negative to count from the last character in the string. Implements
  103. * part of the ArrayAccess interface.
  104. *
  105. * @param mixed $offset The index to check
  106. * @return boolean Whether or not the index exists
  107. */
  108. public function offsetExists($offset)
  109. {
  110. $length = $this->length();
  111. $offset = (int) $offset;
  112. if ($offset >= 0) {
  113. return ($length > $offset);
  114. }
  115. return ($length >= abs($offset));
  116. }
  117. /**
  118. * Returns the character at the given index. Offsets may be negative to
  119. * count from the last character in the string. Implements part of the
  120. * ArrayAccess interface, and throws an OutOfBoundsException if the index
  121. * does not exist.
  122. *
  123. * @param mixed $offset The index from which to retrieve the char
  124. * @return mixed The character at the specified index
  125. * @throws \OutOfBoundsException If the positive or negative offset does
  126. * not exist
  127. */
  128. public function offsetGet($offset)
  129. {
  130. $offset = (int) $offset;
  131. $length = $this->length();
  132. if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) {
  133. throw new \OutOfBoundsException('No character exists at the index');
  134. }
  135. return mb_substr($this->str, $offset, 1, $this->encoding);
  136. }
  137. /**
  138. * Implements part of the ArrayAccess interface, but throws an exception
  139. * when called. This maintains the immutability of Stringy objects.
  140. *
  141. * @param mixed $offset The index of the character
  142. * @param mixed $value Value to set
  143. * @throws \Exception When called
  144. */
  145. public function offsetSet($offset, $value)
  146. {
  147. // Stringy is immutable, cannot directly set char
  148. throw new \Exception('Stringy object is immutable, cannot modify char');
  149. }
  150. /**
  151. * Implements part of the ArrayAccess interface, but throws an exception
  152. * when called. This maintains the immutability of Stringy objects.
  153. *
  154. * @param mixed $offset The index of the character
  155. * @throws \Exception When called
  156. */
  157. public function offsetUnset($offset)
  158. {
  159. // Don't allow directly modifying the string
  160. throw new \Exception('Stringy object is immutable, cannot unset char');
  161. }
  162. /**
  163. * Returns an array consisting of the characters in the string.
  164. *
  165. * @return array An array of string chars
  166. */
  167. public function chars()
  168. {
  169. $chars = array();
  170. for ($i = 0, $l = $this->length(); $i < $l; $i++) {
  171. $chars[] = $this->at($i)->str;
  172. }
  173. return $chars;
  174. }
  175. /**
  176. * Converts the first character of the supplied string to upper case.
  177. *
  178. * @return Stringy Object with the first character of $str being upper case
  179. */
  180. public function upperCaseFirst()
  181. {
  182. $first = mb_substr($this->str, 0, 1, $this->encoding);
  183. $rest = mb_substr($this->str, 1, $this->length() - 1,
  184. $this->encoding);
  185. $str = mb_strtoupper($first, $this->encoding) . $rest;
  186. return static::create($str, $this->encoding);
  187. }
  188. /**
  189. * Converts the first character of the string to lower case.
  190. *
  191. * @return Stringy Object with the first character of $str being lower case
  192. */
  193. public function lowerCaseFirst()
  194. {
  195. $first = mb_substr($this->str, 0, 1, $this->encoding);
  196. $rest = mb_substr($this->str, 1, $this->length() - 1,
  197. $this->encoding);
  198. $str = mb_strtolower($first, $this->encoding) . $rest;
  199. return static::create($str, $this->encoding);
  200. }
  201. /**
  202. * Returns a camelCase version of the string. Trims surrounding spaces,
  203. * capitalizes letters following digits, spaces, dashes and underscores,
  204. * and removes spaces, dashes, as well as underscores.
  205. *
  206. * @return Stringy Object with $str in camelCase
  207. */
  208. public function camelize()
  209. {
  210. $encoding = $this->encoding;
  211. $stringy = $this->trim()->lowerCaseFirst();
  212. $camelCase = preg_replace_callback(
  213. '/[-_\s]+(.)?/u',
  214. function ($match) use ($encoding) {
  215. return $match[1] ? mb_strtoupper($match[1], $encoding) : '';
  216. },
  217. $stringy->str
  218. );
  219. $stringy->str = preg_replace_callback(
  220. '/[\d]+(.)?/u',
  221. function ($match) use ($encoding) {
  222. return mb_strtoupper($match[0], $encoding);
  223. },
  224. $camelCase
  225. );
  226. return $stringy;
  227. }
  228. /**
  229. * Returns an UpperCamelCase version of the supplied string. It trims
  230. * surrounding spaces, capitalizes letters following digits, spaces, dashes
  231. * and underscores, and removes spaces, dashes, underscores.
  232. *
  233. * @return Stringy Object with $str in UpperCamelCase
  234. */
  235. public function upperCamelize()
  236. {
  237. return $this->camelize()->upperCaseFirst();
  238. }
  239. /**
  240. * Returns a lowercase and trimmed string separated by dashes. Dashes are
  241. * inserted before uppercase characters (with the exception of the first
  242. * character of the string), and in place of spaces as well as underscores.
  243. *
  244. * @return Stringy Object with a dasherized $str
  245. */
  246. public function dasherize()
  247. {
  248. return $this->applyDelimiter('-');
  249. }
  250. /**
  251. * Returns a lowercase and trimmed string separated by underscores.
  252. * Underscores are inserted before uppercase characters (with the exception
  253. * of the first character of the string), and in place of spaces as well as
  254. * dashes.
  255. *
  256. * @return Stringy Object with an underscored $str
  257. */
  258. public function underscored()
  259. {
  260. return $this->applyDelimiter('_');
  261. }
  262. /**
  263. * Returns a lowercase and trimmed string separated by the given delimiter.
  264. *
  265. * @param string $delimiter Sequence used to separate parts of the string
  266. * @return Stringy Object with a delimited $str
  267. */
  268. protected function applyDelimiter($delimiter)
  269. {
  270. // Save current regex encoding so we can reset it after
  271. $regexEncoding = mb_regex_encoding();
  272. mb_regex_encoding($this->encoding);
  273. $str = mb_ereg_replace('\B([A-Z])', $delimiter .'\1', $this->trim());
  274. $str = mb_ereg_replace('[-_\s]+', $delimiter, $str);
  275. $str = mb_strtolower($str, $this->encoding);
  276. mb_regex_encoding($regexEncoding);
  277. return static::create($str, $this->encoding);
  278. }
  279. /**
  280. * Returns a case swapped version of the string.
  281. *
  282. * @return Stringy Object whose $str has each character's case swapped
  283. */
  284. public function swapCase()
  285. {
  286. $stringy = static::create($this->str, $this->encoding);
  287. $encoding = $stringy->encoding;
  288. $stringy->str = preg_replace_callback(
  289. '/[\S]/u',
  290. function ($match) use ($encoding) {
  291. if ($match[0] == mb_strtoupper($match[0], $encoding)) {
  292. return mb_strtolower($match[0], $encoding);
  293. } else {
  294. return mb_strtoupper($match[0], $encoding);
  295. }
  296. },
  297. $stringy->str
  298. );
  299. return $stringy;
  300. }
  301. /**
  302. * Returns a trimmed string with the first letter of each word capitalized.
  303. * Ignores the case of other letters, preserving any acronyms. Also accepts
  304. * an array, $ignore, allowing you to list words not to be capitalized.
  305. *
  306. * @param array $ignore An array of words not to capitalize
  307. * @return Stringy Object with a titleized $str
  308. */
  309. public function titleize($ignore = null)
  310. {
  311. $buffer = $this->trim();
  312. $encoding = $this->encoding;
  313. $buffer = preg_replace_callback(
  314. '/([\S]+)/u',
  315. function ($match) use ($encoding, $ignore) {
  316. if ($ignore && in_array($match[0], $ignore)) {
  317. return $match[0];
  318. } else {
  319. $stringy = new Stringy($match[0], $encoding);
  320. return (string) $stringy->upperCaseFirst();
  321. }
  322. },
  323. $buffer
  324. );
  325. return new Stringy($buffer, $encoding);
  326. }
  327. /**
  328. * Capitalizes the first word of the string, replaces underscores with
  329. * spaces, and strips '_id'.
  330. *
  331. * @return Stringy Object with a humanized $str
  332. */
  333. public function humanize()
  334. {
  335. $str = str_replace(array('_id', '_'), array('', ' '), $this->str);
  336. return static::create($str, $this->encoding)->trim()->upperCaseFirst();
  337. }
  338. /**
  339. * Returns a string with smart quotes, ellipsis characters, and dashes from
  340. * Windows-1252 (commonly used in Word documents) replaced by their ASCII
  341. * equivalents.
  342. *
  343. * @return Stringy Object whose $str has those characters removed
  344. */
  345. public function tidy()
  346. {
  347. $str = preg_replace(array(
  348. '/\x{2026}/u',
  349. '/[\x{201C}\x{201D}]/u',
  350. '/[\x{2018}\x{2019}]/u',
  351. '/[\x{2013}\x{2014}]/u',
  352. ), array(
  353. '...',
  354. '"',
  355. "'",
  356. '-',
  357. ), $this->str);
  358. return static::create($str, $this->encoding);
  359. }
  360. /**
  361. * Trims the string and replaces consecutive whitespace characters with a
  362. * single space. This includes tabs and newline characters, as well as
  363. * multibyte whitespace such as the thin space and ideographic space.
  364. *
  365. * @return Stringy Object with a trimmed $str and condensed whitespace
  366. */
  367. public function collapseWhitespace()
  368. {
  369. return $this->regexReplace('[[:space:]]+', ' ')->trim();
  370. }
  371. /**
  372. * Returns an ASCII version of the string. A set of non-ASCII characters are
  373. * replaced with their closest ASCII counterparts, and the rest are removed
  374. * unless instructed otherwise.
  375. *
  376. * @param bool $removeUnsupported Whether or not to remove the
  377. * unsupported characters
  378. * @return Stringy Object whose $str contains only ASCII characters
  379. */
  380. public function toAscii($removeUnsupported = true)
  381. {
  382. $str = $this->str;
  383. foreach ($this->charsArray() as $key => $value) {
  384. $str = str_replace($value, $key, $str);
  385. }
  386. if ($removeUnsupported) {
  387. $str = preg_replace('/[^\x20-\x7E]/u', '', $str);
  388. }
  389. return static::create($str, $this->encoding);
  390. }
  391. /**
  392. * Returns the replacements for the toAscii() method.
  393. *
  394. * @return array An array of replacements.
  395. */
  396. protected function charsArray()
  397. {
  398. static $charsArray;
  399. if (isset($charsArray)) return $charsArray;
  400. return $charsArray = array(
  401. 'a' => array(
  402. 'à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ',
  403. 'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ä', 'ā', 'ą',
  404. 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ',
  405. 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ',
  406. 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ'),
  407. 'b' => array('б', 'β', 'Ъ', 'Ь', 'ب'),
  408. 'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
  409. 'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ',
  410. 'д', 'δ', 'د', 'ض'),
  411. 'e' => array('é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ',
  412. 'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ',
  413. 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э',
  414. 'є', 'ə'),
  415. 'f' => array('ф', 'φ', 'ف'),
  416. 'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ج'),
  417. 'h' => array('ĥ', 'ħ', 'η', 'ή', 'ح', 'ه'),
  418. 'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į',
  419. 'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ',
  420. 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ',
  421. 'ῗ', 'і', 'ї', 'и'),
  422. 'j' => array('ĵ', 'ј', 'Ј'),
  423. 'k' => array('ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك'),
  424. 'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل'),
  425. 'm' => array('м', 'μ', 'م'),
  426. 'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن'),
  427. 'o' => array('ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ',
  428. 'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő',
  429. 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό',
  430. 'ö', 'о', 'و', 'θ'),
  431. 'p' => array('п', 'π'),
  432. 'r' => array('ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر'),
  433. 's' => array('ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص'),
  434. 't' => array('ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط'),
  435. 'u' => array('ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ',
  436. 'ự', 'ü', 'û', 'ū', 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у'),
  437. 'v' => array('в'),
  438. 'w' => array('ŵ', 'ω', 'ώ'),
  439. 'x' => array('χ'),
  440. 'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ',
  441. 'ϋ', 'ύ', 'ΰ', 'ي'),
  442. 'z' => array('ź', 'ž', 'ż', 'з', 'ζ', 'ز'),
  443. 'aa' => array('ع'),
  444. 'ae' => array('æ'),
  445. 'ch' => array('ч'),
  446. 'dj' => array('ђ', 'đ'),
  447. 'dz' => array('џ'),
  448. 'gh' => array('غ'),
  449. 'kh' => array('х', 'خ'),
  450. 'lj' => array('љ'),
  451. 'nj' => array('њ'),
  452. 'oe' => array('œ'),
  453. 'ps' => array('ψ'),
  454. 'sh' => array('ш'),
  455. 'shch' => array('щ'),
  456. 'ss' => array('ß'),
  457. 'th' => array('þ', 'ث', 'ذ', 'ظ'),
  458. 'ts' => array('ц'),
  459. 'ya' => array('я'),
  460. 'yu' => array('ю'),
  461. 'zh' => array('ж'),
  462. '(c)' => array('©'),
  463. 'A' => array('Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ',
  464. 'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Ä', 'Å', 'Ā',
  465. 'Ą', 'Α', 'Ά', 'Ἀ', 'Ἁ', 'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ',
  466. 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ', 'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ',
  467. 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'),
  468. 'B' => array('Б', 'Β'),
  469. 'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
  470. 'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д', 'Δ'),
  471. 'E' => array('É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ế', 'Ề', 'Ể', 'Ễ',
  472. 'Ệ', 'Ë', 'Ē', 'Ę', 'Ě', 'Ĕ', 'Ė', 'Ε', 'Έ', 'Ἐ',
  473. 'Ἑ', 'Ἒ', 'Ἓ', 'Ἔ', 'Ἕ', 'Έ', 'Ὲ', 'Е', 'Ё', 'Э',
  474. 'Є', 'Ə'),
  475. 'F' => array('Ф', 'Φ'),
  476. 'G' => array('Ğ', 'Ġ', 'Ģ', 'Г', 'Ґ', 'Γ'),
  477. 'H' => array('Η', 'Ή'),
  478. 'I' => array('Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'Î', 'Ï', 'Ī', 'Ĭ', 'Į',
  479. 'İ', 'Ι', 'Ί', 'Ϊ', 'Ἰ', 'Ἱ', 'Ἳ', 'Ἴ', 'Ἵ', 'Ἶ',
  480. 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И', 'І', 'Ї'),
  481. 'K' => array('К', 'Κ'),
  482. 'L' => array('Ĺ', 'Ł', 'Л', 'Λ', 'Ļ'),
  483. 'M' => array('М', 'Μ'),
  484. 'N' => array('Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν'),
  485. 'O' => array('Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ',
  486. 'Ộ', 'Ơ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'Ö', 'Ø', 'Ō',
  487. 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ', 'Ὃ', 'Ὄ', 'Ὅ',
  488. 'Ὸ', 'Ό', 'О', 'Θ', 'Ө'),
  489. 'P' => array('П', 'Π'),
  490. 'R' => array('Ř', 'Ŕ', 'Р', 'Ρ'),
  491. 'S' => array('Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ'),
  492. 'T' => array('Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ'),
  493. 'U' => array('Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ',
  494. 'Ự', 'Û', 'Ü', 'Ū', 'Ů', 'Ű', 'Ŭ', 'Ų', 'У'),
  495. 'V' => array('В'),
  496. 'W' => array('Ω', 'Ώ'),
  497. 'X' => array('Χ'),
  498. 'Y' => array('Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ',
  499. 'Ы', 'Й', 'Υ', 'Ϋ'),
  500. 'Z' => array('Ź', 'Ž', 'Ż', 'З', 'Ζ'),
  501. 'AE' => array('Æ'),
  502. 'CH' => array('Ч'),
  503. 'DJ' => array('Ђ'),
  504. 'DZ' => array('Џ'),
  505. 'KH' => array('Х'),
  506. 'LJ' => array('Љ'),
  507. 'NJ' => array('Њ'),
  508. 'PS' => array('Ψ'),
  509. 'SH' => array('Ш'),
  510. 'SHCH' => array('Щ'),
  511. 'SS' => array('ẞ'),
  512. 'TH' => array('Þ'),
  513. 'TS' => array('Ц'),
  514. 'YA' => array('Я'),
  515. 'YU' => array('Ю'),
  516. 'ZH' => array('Ж'),
  517. ' ' => array("\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81",
  518. "\xE2\x80\x82", "\xE2\x80\x83", "\xE2\x80\x84",
  519. "\xE2\x80\x85", "\xE2\x80\x86", "\xE2\x80\x87",
  520. "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A",
  521. "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80"),
  522. );
  523. }
  524. /**
  525. * Pads the string to a given length with $padStr. If length is less than
  526. * or equal to the length of the string, no padding takes places. The
  527. * default string used for padding is a space, and the default type (one of
  528. * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
  529. * if $padType isn't one of those 3 values.
  530. *
  531. * @param int $length Desired string length after padding
  532. * @param string $padStr String used to pad, defaults to space
  533. * @param string $padType One of 'left', 'right', 'both'
  534. * @return Stringy Object with a padded $str
  535. * @throws InvalidArgumentException If $padType isn't one of 'right',
  536. * 'left' or 'both'
  537. */
  538. public function pad($length, $padStr = ' ', $padType = 'right')
  539. {
  540. if (!in_array($padType, array('left', 'right', 'both'))) {
  541. throw new \InvalidArgumentException('Pad expects $padType ' .
  542. "to be one of 'left', 'right' or 'both'");
  543. }
  544. switch ($padType) {
  545. case 'left':
  546. return $this->padLeft($length, $padStr);
  547. case 'right':
  548. return $this->padRight($length, $padStr);
  549. default:
  550. return $this->padBoth($length, $padStr);
  551. }
  552. }
  553. /**
  554. * Returns a new string of a given length such that the beginning of the
  555. * string is padded. Alias for pad() with a $padType of 'left'.
  556. *
  557. * @param int $length Desired string length after padding
  558. * @param string $padStr String used to pad, defaults to space
  559. * @return Stringy String with left padding
  560. */
  561. public function padLeft($length, $padStr = ' ')
  562. {
  563. return $this->applyPadding($length - $this->length(), 0, $padStr);
  564. }
  565. /**
  566. * Returns a new string of a given length such that the end of the string
  567. * is padded. Alias for pad() with a $padType of 'right'.
  568. *
  569. * @param int $length Desired string length after padding
  570. * @param string $padStr String used to pad, defaults to space
  571. * @return Stringy String with right padding
  572. */
  573. public function padRight($length, $padStr = ' ')
  574. {
  575. return $this->applyPadding(0, $length - $this->length(), $padStr);
  576. }
  577. /**
  578. * Returns a new string of a given length such that both sides of the
  579. * string are padded. Alias for pad() with a $padType of 'both'.
  580. *
  581. * @param int $length Desired string length after padding
  582. * @param string $padStr String used to pad, defaults to space
  583. * @return Stringy String with padding applied
  584. */
  585. public function padBoth($length, $padStr = ' ')
  586. {
  587. $padding = $length - $this->length();
  588. return $this->applyPadding(floor($padding / 2), ceil($padding / 2),
  589. $padStr);
  590. }
  591. /**
  592. * Adds the specified amount of left and right padding to the given string.
  593. * The default character used is a space.
  594. *
  595. * @param int $left Length of left padding
  596. * @param int $right Length of right padding
  597. * @param string $padStr String used to pad
  598. * @return Stringy String with padding applied
  599. */
  600. private function applyPadding($left = 0, $right = 0, $padStr = ' ')
  601. {
  602. $stringy = static::create($this->str, $this->encoding);
  603. $length = mb_strlen($padStr, $stringy->encoding);
  604. $strLength = $stringy->length();
  605. $paddedLength = $strLength + $left + $right;
  606. if (!$length || $paddedLength <= $strLength) {
  607. return $stringy;
  608. }
  609. $leftPadding = mb_substr(str_repeat($padStr, ceil($left / $length)), 0,
  610. $left, $stringy->encoding);
  611. $rightPadding = mb_substr(str_repeat($padStr, ceil($right / $length)),
  612. 0, $right, $stringy->encoding);
  613. $stringy->str = $leftPadding . $stringy->str . $rightPadding;
  614. return $stringy;
  615. }
  616. /**
  617. * Returns true if the string begins with $substring, false otherwise. By
  618. * default, the comparison is case-sensitive, but can be made insensitive
  619. * by setting $caseSensitive to false.
  620. *
  621. * @param string $substring The substring to look for
  622. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  623. * @return bool Whether or not $str starts with $substring
  624. */
  625. public function startsWith($substring, $caseSensitive = true)
  626. {
  627. $substringLength = mb_strlen($substring, $this->encoding);
  628. $startOfStr = mb_substr($this->str, 0, $substringLength,
  629. $this->encoding);
  630. if (!$caseSensitive) {
  631. $substring = mb_strtolower($substring, $this->encoding);
  632. $startOfStr = mb_strtolower($startOfStr, $this->encoding);
  633. }
  634. return (string) $substring === $startOfStr;
  635. }
  636. /**
  637. * Returns true if the string ends with $substring, false otherwise. By
  638. * default, the comparison is case-sensitive, but can be made insensitive
  639. * by setting $caseSensitive to false.
  640. *
  641. * @param string $substring The substring to look for
  642. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  643. * @return bool Whether or not $str ends with $substring
  644. */
  645. public function endsWith($substring, $caseSensitive = true)
  646. {
  647. $substringLength = mb_strlen($substring, $this->encoding);
  648. $strLength = $this->length();
  649. $endOfStr = mb_substr($this->str, $strLength - $substringLength,
  650. $substringLength, $this->encoding);
  651. if (!$caseSensitive) {
  652. $substring = mb_strtolower($substring, $this->encoding);
  653. $endOfStr = mb_strtolower($endOfStr, $this->encoding);
  654. }
  655. return (string) $substring === $endOfStr;
  656. }
  657. /**
  658. * Converts each tab in the string to some number of spaces, as defined by
  659. * $tabLength. By default, each tab is converted to 4 consecutive spaces.
  660. *
  661. * @param int $tabLength Number of spaces to replace each tab with
  662. * @return Stringy Object whose $str has had tabs switched to spaces
  663. */
  664. public function toSpaces($tabLength = 4)
  665. {
  666. $spaces = str_repeat(' ', $tabLength);
  667. $str = str_replace("\t", $spaces, $this->str);
  668. return static::create($str, $this->encoding);
  669. }
  670. /**
  671. * Converts each occurrence of some consecutive number of spaces, as
  672. * defined by $tabLength, to a tab. By default, each 4 consecutive spaces
  673. * are converted to a tab.
  674. *
  675. * @param int $tabLength Number of spaces to replace with a tab
  676. * @return Stringy Object whose $str has had spaces switched to tabs
  677. */
  678. public function toTabs($tabLength = 4)
  679. {
  680. $spaces = str_repeat(' ', $tabLength);
  681. $str = str_replace($spaces, "\t", $this->str);
  682. return static::create($str, $this->encoding);
  683. }
  684. /**
  685. * Converts the first character of each word in the string to uppercase.
  686. *
  687. * @return Stringy Object with all characters of $str being title-cased
  688. */
  689. public function toTitleCase()
  690. {
  691. $str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding);
  692. return static::create($str, $this->encoding);
  693. }
  694. /**
  695. * Converts all characters in the string to lowercase. An alias for PHP's
  696. * mb_strtolower().
  697. *
  698. * @return Stringy Object with all characters of $str being lowercase
  699. */
  700. public function toLowerCase()
  701. {
  702. $str = mb_strtolower($this->str, $this->encoding);
  703. return static::create($str, $this->encoding);
  704. }
  705. /**
  706. * Converts all characters in the string to uppercase. An alias for PHP's
  707. * mb_strtoupper().
  708. *
  709. * @return Stringy Object with all characters of $str being uppercase
  710. */
  711. public function toUpperCase()
  712. {
  713. $str = mb_strtoupper($this->str, $this->encoding);
  714. return static::create($str, $this->encoding);
  715. }
  716. /**
  717. * Converts the string into an URL slug. This includes replacing non-ASCII
  718. * characters with their closest ASCII equivalents, removing remaining
  719. * non-ASCII and non-alphanumeric characters, and replacing whitespace with
  720. * $replacement. The replacement defaults to a single dash, and the string
  721. * is also converted to lowercase.
  722. *
  723. * @param string $replacement The string used to replace whitespace
  724. * @return Stringy Object whose $str has been converted to an URL slug
  725. */
  726. public function slugify($replacement = '-')
  727. {
  728. $stringy = $this->toAscii();
  729. $quotedReplacement = preg_quote($replacement);
  730. $pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
  731. $stringy->str = preg_replace($pattern, '', $stringy);
  732. return $stringy->toLowerCase()->applyDelimiter($replacement)
  733. ->removeLeft($replacement)->removeRight($replacement);
  734. }
  735. /**
  736. * Returns true if the string contains $needle, false otherwise. By default
  737. * the comparison is case-sensitive, but can be made insensitive by setting
  738. * $caseSensitive to false.
  739. *
  740. * @param string $needle Substring to look for
  741. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  742. * @return bool Whether or not $str contains $needle
  743. */
  744. public function contains($needle, $caseSensitive = true)
  745. {
  746. $encoding = $this->encoding;
  747. if ($caseSensitive) {
  748. return (mb_strpos($this->str, $needle, 0, $encoding) !== false);
  749. } else {
  750. return (mb_stripos($this->str, $needle, 0, $encoding) !== false);
  751. }
  752. }
  753. /**
  754. * Returns true if the string contains any $needles, false otherwise. By
  755. * default the comparison is case-sensitive, but can be made insensitive by
  756. * setting $caseSensitive to false.
  757. *
  758. * @param array $needles Substrings to look for
  759. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  760. * @return bool Whether or not $str contains $needle
  761. */
  762. public function containsAny($needles, $caseSensitive = true)
  763. {
  764. if (empty($needles)) {
  765. return false;
  766. }
  767. foreach ($needles as $needle) {
  768. if ($this->contains($needle, $caseSensitive)) {
  769. return true;
  770. }
  771. }
  772. return false;
  773. }
  774. /**
  775. * Returns true if the string contains all $needles, false otherwise. By
  776. * default the comparison is case-sensitive, but can be made insensitive by
  777. * setting $caseSensitive to false.
  778. *
  779. * @param array $needles Substrings to look for
  780. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  781. * @return bool Whether or not $str contains $needle
  782. */
  783. public function containsAll($needles, $caseSensitive = true)
  784. {
  785. if (empty($needles)) {
  786. return false;
  787. }
  788. foreach ($needles as $needle) {
  789. if (!$this->contains($needle, $caseSensitive)) {
  790. return false;
  791. }
  792. }
  793. return true;
  794. }
  795. /**
  796. * Surrounds $str with the given substring.
  797. *
  798. * @param string $substring The substring to add to both sides
  799. * @return Stringy Object whose $str had the substring both prepended and
  800. * appended
  801. */
  802. public function surround($substring)
  803. {
  804. $str = implode('', array($substring, $this->str, $substring));
  805. return static::create($str, $this->encoding);
  806. }
  807. /**
  808. * Inserts $substring into the string at the $index provided.
  809. *
  810. * @param string $substring String to be inserted
  811. * @param int $index The index at which to insert the substring
  812. * @return Stringy Object with the resulting $str after the insertion
  813. */
  814. public function insert($substring, $index)
  815. {
  816. $stringy = static::create($this->str, $this->encoding);
  817. if ($index > $stringy->length()) {
  818. return $stringy;
  819. }
  820. $start = mb_substr($stringy->str, 0, $index, $stringy->encoding);
  821. $end = mb_substr($stringy->str, $index, $stringy->length(),
  822. $stringy->encoding);
  823. $stringy->str = $start . $substring . $end;
  824. return $stringy;
  825. }
  826. /**
  827. * Truncates the string to a given length. If $substring is provided, and
  828. * truncating occurs, the string is further truncated so that the substring
  829. * may be appended without exceeding the desired length.
  830. *
  831. * @param int $length Desired length of the truncated string
  832. * @param string $substring The substring to append if it can fit
  833. * @return Stringy Object with the resulting $str after truncating
  834. */
  835. public function truncate($length, $substring = '')
  836. {
  837. $stringy = static::create($this->str, $this->encoding);
  838. if ($length >= $stringy->length()) {
  839. return $stringy;
  840. }
  841. // Need to further trim the string so we can append the substring
  842. $substringLength = mb_strlen($substring, $stringy->encoding);
  843. $length = $length - $substringLength;
  844. $truncated = mb_substr($stringy->str, 0, $length, $stringy->encoding);
  845. $stringy->str = $truncated . $substring;
  846. return $stringy;
  847. }
  848. /**
  849. * Truncates the string to a given length, while ensuring that it does not
  850. * split words. If $substring is provided, and truncating occurs, the
  851. * string is further truncated so that the substring may be appended without
  852. * exceeding the desired length.
  853. *
  854. * @param int $length Desired length of the truncated string
  855. * @param string $substring The substring to append if it can fit
  856. * @return Stringy Object with the resulting $str after truncating
  857. */
  858. public function safeTruncate($length, $substring = '')
  859. {
  860. $stringy = static::create($this->str, $this->encoding);
  861. if ($length >= $stringy->length()) {
  862. return $stringy;
  863. }
  864. // Need to further trim the string so we can append the substring
  865. $encoding = $stringy->encoding;
  866. $substringLength = mb_strlen($substring, $encoding);
  867. $length = $length - $substringLength;
  868. $truncated = mb_substr($stringy->str, 0, $length, $encoding);
  869. // If the last word was truncated
  870. if (mb_strpos($stringy->str, ' ', $length - 1, $encoding) != $length) {
  871. // Find pos of the last occurrence of a space, get up to that
  872. $lastPos = mb_strrpos($truncated, ' ', 0, $encoding);
  873. $truncated = mb_substr($truncated, 0, $lastPos, $encoding);
  874. }
  875. $stringy->str = $truncated . $substring;
  876. return $stringy;
  877. }
  878. /**
  879. * Returns a reversed string. A multibyte version of strrev().
  880. *
  881. * @return Stringy Object with a reversed $str
  882. */
  883. public function reverse()
  884. {
  885. $strLength = $this->length();
  886. $reversed = '';
  887. // Loop from last index of string to first
  888. for ($i = $strLength - 1; $i >= 0; $i--) {
  889. $reversed .= mb_substr($this->str, $i, 1, $this->encoding);
  890. }
  891. return static::create($reversed, $this->encoding);
  892. }
  893. /**
  894. * A multibyte str_shuffle() function. It returns a string with its
  895. * characters in random order.
  896. *
  897. * @return Stringy Object with a shuffled $str
  898. */
  899. public function shuffle()
  900. {
  901. $indexes = range(0, $this->length() - 1);
  902. shuffle($indexes);
  903. $shuffledStr = '';
  904. foreach ($indexes as $i) {
  905. $shuffledStr .= mb_substr($this->str, $i, 1, $this->encoding);
  906. }
  907. return static::create($shuffledStr, $this->encoding);
  908. }
  909. /**
  910. * Returns the trimmed string. An alias for PHP's trim() function.
  911. *
  912. * @return Stringy Object with a trimmed $str
  913. */
  914. public function trim()
  915. {
  916. return static::create(trim($this->str), $this->encoding);
  917. }
  918. /**
  919. * Returns the longest common prefix between the string and $otherStr.
  920. *
  921. * @param string $otherStr Second string for comparison
  922. * @return Stringy Object with its $str being the longest common prefix
  923. */
  924. public function longestCommonPrefix($otherStr)
  925. {
  926. $encoding = $this->encoding;
  927. $maxLength = min($this->length(), mb_strlen($otherStr, $encoding));
  928. $longestCommonPrefix = '';
  929. for ($i = 0; $i < $maxLength; $i++) {
  930. $char = mb_substr($this->str, $i, 1, $encoding);
  931. if ($char == mb_substr($otherStr, $i, 1, $encoding)) {
  932. $longestCommonPrefix .= $char;
  933. } else {
  934. break;
  935. }
  936. }
  937. return static::create($longestCommonPrefix, $encoding);
  938. }
  939. /**
  940. * Returns the longest common suffix between the string and $otherStr.
  941. *
  942. * @param string $otherStr Second string for comparison
  943. * @return Stringy Object with its $str being the longest common suffix
  944. */
  945. public function longestCommonSuffix($otherStr)
  946. {
  947. $encoding = $this->encoding;
  948. $maxLength = min($this->length(), mb_strlen($otherStr, $encoding));
  949. $longestCommonSuffix = '';
  950. for ($i = 1; $i <= $maxLength; $i++) {
  951. $char = mb_substr($this->str, -$i, 1, $encoding);
  952. if ($char == mb_substr($otherStr, -$i, 1, $encoding)) {
  953. $longestCommonSuffix = $char . $longestCommonSuffix;
  954. } else {
  955. break;
  956. }
  957. }
  958. return static::create($longestCommonSuffix, $encoding);
  959. }
  960. /**
  961. * Returns the longest common substring between the string and $otherStr.
  962. * In the case of ties, it returns that which occurs first.
  963. *
  964. * @param string $otherStr Second string for comparison
  965. * @return Stringy Object with its $str being the longest common substring
  966. */
  967. public function longestCommonSubstring($otherStr)
  968. {
  969. // Uses dynamic programming to solve
  970. // http://en.wikipedia.org/wiki/Longest_common_substring_problem
  971. $encoding = $this->encoding;
  972. $stringy = static::create($this->str, $encoding);
  973. $strLength = $stringy->length();
  974. $otherLength = mb_strlen($otherStr, $encoding);
  975. // Return if either string is empty
  976. if ($strLength == 0 || $otherLength == 0) {
  977. $stringy->str = '';
  978. return $stringy;
  979. }
  980. $len = 0;
  981. $end = 0;
  982. $table = array_fill(0, $strLength + 1,
  983. array_fill(0, $otherLength + 1, 0));
  984. for ($i = 1; $i <= $strLength; $i++) {
  985. for ($j = 1; $j <= $otherLength; $j++) {
  986. $strChar = mb_substr($stringy->str, $i - 1, 1, $encoding);
  987. $otherChar = mb_substr($otherStr, $j - 1, 1, $encoding);
  988. if ($strChar == $otherChar) {
  989. $table[$i][$j] = $table[$i - 1][$j - 1] + 1;
  990. if ($table[$i][$j] > $len) {
  991. $len = $table[$i][$j];
  992. $end = $i;
  993. }
  994. } else {
  995. $table[$i][$j] = 0;
  996. }
  997. }
  998. }
  999. $stringy->str = mb_substr($stringy->str, $end - $len, $len, $encoding);
  1000. return $stringy;
  1001. }
  1002. /**
  1003. * Returns the length of the string. An alias for PHP's mb_strlen() function.
  1004. *
  1005. * @return int The number of characters in $str given the encoding
  1006. */
  1007. public function length()
  1008. {
  1009. return mb_strlen($this->str, $this->encoding);
  1010. }
  1011. /**
  1012. * Returns the substring beginning at $start with the specified $length.
  1013. * It differs from the mb_substr() function in that providing a $length of
  1014. * null will return the rest of the string, rather than an empty string.
  1015. *
  1016. * @param int $start Position of the first character to use
  1017. * @param int $length Maximum number of characters used
  1018. * @return Stringy Object with its $str being the substring
  1019. */
  1020. public function substr($start, $length = null)
  1021. {
  1022. $length = $length === null ? $this->length() : $length;
  1023. $str = mb_substr($this->str, $start, $length, $this->encoding);
  1024. return static::create($str, $this->encoding);
  1025. }
  1026. /**
  1027. * Returns the character at $index, with indexes starting at 0.
  1028. *
  1029. * @param int $index Position of the character
  1030. * @return Stringy The character at $index
  1031. */
  1032. public function at($index)
  1033. {
  1034. return $this->substr($index, 1);
  1035. }
  1036. /**
  1037. * Returns the first $n characters of the string.
  1038. *
  1039. * @param int $n Number of characters to retrieve from the start
  1040. * @return Stringy Object with its $str being the first $n chars
  1041. */
  1042. public function first($n)
  1043. {
  1044. $stringy = static::create($this->str, $this->encoding);
  1045. if ($n < 0) {
  1046. $stringy->str = '';
  1047. } else {
  1048. return $stringy->substr(0, $n);
  1049. }
  1050. return $stringy;
  1051. }
  1052. /**
  1053. * Returns the last $n characters of the string.
  1054. *
  1055. * @param int $n Number of characters to retrieve from the end
  1056. * @return Stringy Object with its $str being the last $n chars
  1057. */
  1058. public function last($n)
  1059. {
  1060. $stringy = static::create($this->str, $this->encoding);
  1061. if ($n <= 0) {
  1062. $stringy->str = '';
  1063. } else {
  1064. return $stringy->substr(-$n);
  1065. }
  1066. return $stringy;
  1067. }
  1068. /**
  1069. * Ensures that the string begins with $substring. If it doesn't, it's
  1070. * prepended.
  1071. *
  1072. * @param string $substring The substring to add if not present
  1073. * @return Stringy Object with its $str prefixed by the $substring
  1074. */
  1075. public function ensureLeft($substring)
  1076. {
  1077. $stringy = static::create($this->str, $this->encoding);
  1078. if (!$stringy->startsWith($substring)) {
  1079. $stringy->str = $substring . $stringy->str;
  1080. }
  1081. return $stringy;
  1082. }
  1083. /**
  1084. * Ensures that the string begins with $substring. If it doesn't, it's
  1085. * appended.
  1086. *
  1087. * @param string $substring The substring to add if not present
  1088. * @return Stringy Object with its $str suffixed by the $substring
  1089. */
  1090. public function ensureRight($substring)
  1091. {
  1092. $stringy = static::create($this->str, $this->encoding);
  1093. if (!$stringy->endsWith($substring)) {
  1094. $stringy->str .= $substring;
  1095. }
  1096. return $stringy;
  1097. }
  1098. /**
  1099. * Returns a new string with the prefix $substring removed, if present.
  1100. *
  1101. * @param string $substring The prefix to remove
  1102. * @return Stringy Object having a $str without the prefix $substring
  1103. */
  1104. public function removeLeft($substring)
  1105. {
  1106. $stringy = static::create($this->str, $this->encoding);
  1107. if ($stringy->startsWith($substring)) {
  1108. $substringLength = mb_strlen($substring, $stringy->encoding);
  1109. return $stringy->substr($substringLength);
  1110. }
  1111. return $stringy;
  1112. }
  1113. /**
  1114. * Returns a new string with the suffix $substring removed, if present.
  1115. *
  1116. * @param string $substring The suffix to remove
  1117. * @return Stringy Object having a $str without the suffix $substring
  1118. */
  1119. public function removeRight($substring)
  1120. {
  1121. $stringy = static::create($this->str, $this->encoding);
  1122. if ($stringy->endsWith($substring)) {
  1123. $substringLength = mb_strlen($substring, $stringy->encoding);
  1124. return $stringy->substr(0, $stringy->length() - $substringLength);
  1125. }
  1126. return $stringy;
  1127. }
  1128. /**
  1129. * Returns true if $str matches the supplied pattern, false otherwise.
  1130. *
  1131. * @param string $pattern Regex pattern to match against
  1132. * @return bool Whether or not $str matches the pattern
  1133. */
  1134. private function matchesPattern($pattern)
  1135. {
  1136. $regexEncoding = mb_regex_encoding();
  1137. mb_regex_encoding($this->encoding);
  1138. $match = mb_ereg_match($pattern, $this->str);
  1139. mb_regex_encoding($regexEncoding);
  1140. return $match;
  1141. }
  1142. /**
  1143. * Returns true if the string contains a lower case char, false
  1144. * otherwise.
  1145. *
  1146. * @return bool Whether or not the string contains a lower case character.
  1147. */
  1148. public function hasLowerCase()
  1149. {
  1150. return $this->matchesPattern('.*[[:lower:]]');
  1151. }
  1152. /**
  1153. * Returns true if the string contains an upper case char, false
  1154. * otherwise.
  1155. *
  1156. * @return bool Whether or not the string contains an upper case character.
  1157. */
  1158. public function hasUpperCase()
  1159. {
  1160. return $this->matchesPattern('.*[[:upper:]]');
  1161. }
  1162. /**
  1163. * Returns true if the string contains only alphabetic chars, false
  1164. * otherwise.
  1165. *
  1166. * @return bool Whether or not $str contains only alphabetic chars
  1167. */
  1168. public function isAlpha()
  1169. {
  1170. return $this->matchesPattern('^[[:alpha:]]*$');
  1171. }
  1172. /**
  1173. * Returns true if the string contains only alphabetic and numeric chars,
  1174. * false otherwise.
  1175. *
  1176. * @return bool Whether or not $str contains only alphanumeric chars
  1177. */
  1178. public function isAlphanumeric()
  1179. {
  1180. return $this->matchesPattern('^[[:alnum:]]*$');
  1181. }
  1182. /**
  1183. * Returns true if the string contains only hexadecimal chars, false
  1184. * otherwise.
  1185. *
  1186. * @return bool Whether or not $str contains only hexadecimal chars
  1187. */
  1188. public function isHexadecimal()
  1189. {
  1190. return $this->matchesPattern('^[[:xdigit:]]*$');
  1191. }
  1192. /**
  1193. * Returns true if the string contains only whitespace chars, false
  1194. * otherwise.
  1195. *
  1196. * @return bool Whether or not $str contains only whitespace characters
  1197. */
  1198. public function isBlank()
  1199. {
  1200. return $this->matchesPattern('^[[:space:]]*$');
  1201. }
  1202. /**
  1203. * Returns true if the string is JSON, false otherwise.
  1204. *
  1205. * @return bool Whether or not $str is JSON
  1206. */
  1207. public function isJson()
  1208. {
  1209. json_decode($this->str);
  1210. return (json_last_error() === JSON_ERROR_NONE);
  1211. }
  1212. /**
  1213. * Returns true if the string contains only lower case chars, false
  1214. * otherwise.
  1215. *
  1216. * @return bool Whether or not $str contains only lower case characters
  1217. */
  1218. public function isLowerCase()
  1219. {
  1220. return $this->matchesPattern('^[[:lower:]]*$');
  1221. }
  1222. /**
  1223. * Returns true if the string contains only lower case chars, false
  1224. * otherwise.
  1225. *
  1226. * @return bool Whether or not $str contains only lower case characters
  1227. */
  1228. public function isUpperCase()
  1229. {
  1230. return $this->matchesPattern('^[[:upper:]]*$');
  1231. }
  1232. /**
  1233. * Returns true if the string is serialized, false otherwise.
  1234. *
  1235. * @return bool Whether or not $str is serialized
  1236. */
  1237. public function isSerialized()
  1238. {
  1239. return $this->str === 'b:0;' || @unserialize($this->str) !== false;
  1240. }
  1241. /**
  1242. * Returns the number of occurrences of $substring in the given string.
  1243. * By default, the comparison is case-sensitive, but can be made insensitive
  1244. * by setting $caseSensitive to false.
  1245. *
  1246. * @param string $substring The substring to search for
  1247. * @param bool $caseSensitive Whether or not to enforce case-sensitivity
  1248. * @return int The number of $substring occurrences
  1249. */
  1250. public function countSubstr($substring, $caseSensitive = true)
  1251. {
  1252. if ($caseSensitive) {
  1253. return mb_substr_count($this->str, $substring, $this->encoding);
  1254. }
  1255. $str = mb_strtoupper($this->str, $this->encoding);
  1256. $substring = mb_strtoupper($substring, $this->encoding);
  1257. return mb_substr_count($str, $substring, $this->encoding);
  1258. }
  1259. /**
  1260. * Replaces all occurrences of $search in $str by $replacement.
  1261. *
  1262. * @param string $search The needle to search for
  1263. * @param string $replacement The string to replace with
  1264. * @return Stringy Object with the resulting $str after the replacements
  1265. */
  1266. public function replace($search, $replacement)
  1267. {
  1268. return $this->regexReplace(preg_quote($search), $replacement);
  1269. }
  1270. /**
  1271. * Replaces all occurrences of $pattern in $str by $replacement. An alias
  1272. * for mb_ereg_replace(). Note that the 'i' option with multibyte patterns
  1273. * in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support
  1274. * in the bundled version of Oniguruma in PHP 5.3.
  1275. *
  1276. * @param string $pattern The regular expression pattern
  1277. * @param string $replacement The string to replace with
  1278. * @param string $options Matching conditions to be used
  1279. * @return Stringy Object with the resulting $str after the replacements
  1280. */
  1281. public function regexReplace($pattern, $replacement, $options = 'msr')
  1282. {
  1283. $regexEncoding = mb_regex_encoding();
  1284. mb_regex_encoding($this->encoding);
  1285. $str = mb_ereg_replace($pattern, $replacement, $this->str, $options);
  1286. mb_regex_encoding($regexEncoding);
  1287. return static::create($str, $this->encoding);
  1288. }
  1289. }