PageRenderTime 71ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/core/string/string.php

https://github.com/geekforbrains/caffeine
PHP | 305 lines | 178 code | 29 blank | 98 comment | 15 complexity | ac3620f1d6e2841e0f6b758118005c24 MD5 | raw file
  1. <?php
  2. class String extends Module {
  3. /**
  4. * TODO
  5. */
  6. private static $_plural = array(
  7. '/(quiz)$/i' => "$1zes",
  8. '/^(ox)$/i' => "$1en",
  9. '/([m|l])ouse$/i' => "$1ice",
  10. '/(matr|vert|ind)ix|ex$/i' => "$1ices",
  11. '/(x|ch|ss|sh)$/i' => "$1es",
  12. '/([^aeiouy]|qu)y$/i' => "$1ies",
  13. '/(hive)$/i' => "$1s",
  14. '/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
  15. '/(shea|lea|loa|thie)f$/i' => "$1ves",
  16. '/sis$/i' => "ses",
  17. '/([ti])um$/i' => "$1a",
  18. '/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
  19. '/(bu)s$/i' => "$1ses",
  20. '/(alias)$/i' => "$1es",
  21. '/(octop)us$/i' => "$1i",
  22. '/(ax|test)is$/i' => "$1es",
  23. '/(us)$/i' => "$1es",
  24. '/s$/i' => "s",
  25. '/$/' => "s"
  26. );
  27. /**
  28. * TODO
  29. */
  30. private static $_singular = array(
  31. '/(quiz)zes$/i' => "$1",
  32. '/(matr)ices$/i' => "$1ix",
  33. '/(vert|ind)ices$/i' => "$1ex",
  34. '/^(ox)en$/i' => "$1",
  35. '/(alias)es$/i' => "$1",
  36. '/(octop|vir)i$/i' => "$1us",
  37. '/(cris|ax|test)es$/i' => "$1is",
  38. '/(shoe)s$/i' => "$1",
  39. '/(o)es$/i' => "$1",
  40. '/(bus)es$/i' => "$1",
  41. '/([m|l])ice$/i' => "$1ouse",
  42. '/(x|ch|ss|sh)es$/i' => "$1",
  43. '/(m)ovies$/i' => "$1ovie",
  44. '/(s)eries$/i' => "$1eries",
  45. '/([^aeiouy]|qu)ies$/i' => "$1y",
  46. '/([lr])ves$/i' => "$1f",
  47. '/(tive)s$/i' => "$1",
  48. '/(hive)s$/i' => "$1",
  49. '/(li|wi|kni)ves$/i' => "$1fe",
  50. '/(shea|loa|lea|thie)ves$/i'=> "$1f",
  51. '/(^analy)ses$/i' => "$1sis",
  52. '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
  53. '/([ti])a$/i' => "$1um",
  54. '/(n)ews$/i' => "$1ews",
  55. '/(h|bl)ouses$/i' => "$1ouse",
  56. '/(corpse)s$/i' => "$1",
  57. '/(us)es$/i' => "$1",
  58. '/s$/i' => ""
  59. );
  60. /**
  61. * TODO
  62. */
  63. private static $_irregular = array(
  64. 'move' => 'moves',
  65. 'foot' => 'feet',
  66. 'goose' => 'geese',
  67. 'sex' => 'sexes',
  68. 'child' => 'children',
  69. 'man' => 'men',
  70. 'tooth' => 'teeth',
  71. 'person' => 'people'
  72. );
  73. /**
  74. * TODO
  75. */
  76. private static $_uncountable = array(
  77. 'sheep',
  78. 'fish',
  79. 'deer',
  80. 'series',
  81. 'species',
  82. 'money',
  83. 'rice',
  84. 'information',
  85. 'equipment'
  86. );
  87. /**
  88. * Converts a singular word to plural.
  89. *
  90. * @param string $string The singular word to convert to plural.
  91. *
  92. * @return string
  93. */
  94. public static function plural($string)
  95. {
  96. // save some time in the case that singular and plural are the same
  97. if(in_array(strtolower($string), self::$_uncountable))
  98. return $string;
  99. // check for irregular singular forms
  100. foreach(self::$_irregular as $pattern => $result)
  101. {
  102. $pattern = '/' . $pattern . '$/i';
  103. if(preg_match($pattern, $string))
  104. return preg_replace($pattern, $result, $string);
  105. }
  106. // check for matches using regular expressions
  107. foreach(self::$_plural as $pattern => $result)
  108. {
  109. if(preg_match($pattern, $string))
  110. return preg_replace($pattern, $result, $string);
  111. }
  112. return $string;
  113. }
  114. /**
  115. * Converts a word from plural to singular.
  116. *
  117. * @param string $string The plural word to convert to singular.
  118. *
  119. * @return string
  120. */
  121. public static function singular($string)
  122. {
  123. // save some time in the case that singular and plural are the same
  124. if(in_array(strtolower($string), self::$_uncountable))
  125. return $string;
  126. // check for irregular plural forms
  127. foreach(self::$_irregular as $result => $pattern)
  128. {
  129. $pattern = '/' . $pattern . '$/i';
  130. if(preg_match($pattern, $string))
  131. return preg_replace($pattern, $result, $string);
  132. }
  133. // check for matches using regular expressions
  134. foreach(self::$_singular as $pattern => $result)
  135. {
  136. if(preg_match($pattern, $string))
  137. return preg_replace($pattern, $result, $string);
  138. }
  139. return $string;
  140. }
  141. /**
  142. * Determines if a given string starts with another string.
  143. *
  144. * @param string $string The string to check the beginning of.
  145. * @param string $start The string to check for at the beginning of $string.
  146. *
  147. * @return boolean
  148. */
  149. public static function startsWith($string, $start)
  150. {
  151. if(substr($string, 0, strlen($start)) == $start)
  152. return true;
  153. return false;
  154. }
  155. /**
  156. * Determines if a string ends with another string.
  157. *
  158. * @param string $string The string to check the end of.
  159. * @param string $end The string to check for at the end of $string.
  160. *
  161. * @return boolean
  162. */
  163. public static function endsWith($string, $end)
  164. {
  165. if(substr($string, -strlen($end)) == $end)
  166. return true;
  167. return false;
  168. }
  169. /**
  170. * Takes a string and returns it in "tag" format. A tag is all lower case
  171. * with only numbers and letters. Words are seperated by a "splitter".
  172. *
  173. * Example:
  174. * "It's a tag!" = "its-a-tag"
  175. * "My, myself & I" = "me-myself-and-i"
  176. *
  177. * @param $string
  178. * The string to convert to a tag.
  179. *
  180. * @param $splitter
  181. * An optional argument to set the splitter (replacement for spaces).
  182. * Defaults to "-".
  183. *
  184. * @return
  185. * Returns the value of $string as a tag.
  186. */
  187. public static function tagify($string, $splitter = '-')
  188. {
  189. $string = trim(strtolower($string));
  190. $string = str_replace('&', 'and', $string);
  191. $string = preg_replace('/[^A-Za-z0-9\s\-]+/', '', $string); // Clear out anything that isn't a letter, number or space
  192. $string = preg_replace('/[\s]+/', '-', $string);
  193. $string = preg_replace('/[\-]{2,}/', '-', $string); // Replace duplicate dashes
  194. return $string;
  195. }
  196. /**
  197. * Alias of tagify.
  198. */
  199. public static function slugify($string, $splitter = '-') {
  200. return self::tagify($string, $splitter);
  201. }
  202. /**
  203. * Takes a string with the tokens %, %s, %d and converts them to regex
  204. * patterns for anything, only words and only number respectively.
  205. */
  206. public static function regify($string)
  207. {
  208. $replacements = array(
  209. '([A-Za-z0-9\-]+)' => array(':slug'),
  210. '([A-Za-z\-]+)' => array('%s', ':alpha', ':abc'),
  211. '([0-9]+)' => array('%d', ':num', ':id'),
  212. '(.*?)' => array('%', ':any')
  213. );
  214. foreach($replacements as $r => $keys)
  215. {
  216. foreach($keys as $k)
  217. {
  218. $string = str_replace($k, $r, $string);
  219. }
  220. }
  221. return $string;
  222. }
  223. /**
  224. * Shortens a string based on the given length to the nearest word. This means
  225. * it wont cut off part of a word, it'll reduce the string until it finds the end of
  226. * a whole word. If the string is shorter than the length, the $append will not be added.
  227. *
  228. * @param string $string The string you want to shorten
  229. * @param int $length The length to shorten the string to, to the nearest word
  230. * @param string $append A string value to append to shortened strings (ex: "...");
  231. * @param boolean $striptags Enables or disables the stripping of HTML tags from the string. Defaults to true.
  232. *
  233. * @return string
  234. */
  235. public static function truncate($string, $length, $append = null, $striptags = true)
  236. {
  237. if($striptags)
  238. $string = strip_tags($string);
  239. if(strlen($string) > $length)
  240. {
  241. $shortened = '';
  242. for($i = $length; $i > 0; $i--)
  243. {
  244. if($string{$i} == ' ')
  245. {
  246. $shortened = substr($string, 0, $i);
  247. return substr($string, 0, $i) . $append;
  248. }
  249. }
  250. }
  251. return $string;
  252. }
  253. /**
  254. * Alias of truncate
  255. */
  256. public static function shorten($string, $length, $append = null, $striptags = true) {
  257. return self::truncate($string, $length, $append, $striptags);
  258. }
  259. /**
  260. * Splits a camel case string by $splitter.
  261. *
  262. * Example:
  263. * FooBar = Foo_Bar
  264. *
  265. * @param string $string The camel case string to split.
  266. * @param string $splitter The string to split the camel case with.
  267. * @return Formatted string
  268. */
  269. public static function splitCamelCase($string, $splitter = '_')
  270. {
  271. $string = str_replace($splitter, '', $string); // Remove previous splitter in string to avoid duplicates
  272. $string[0] = strtolower($string[0]);
  273. $func = create_function('$c', sprintf('return "%s" . strtolower($c[1]);', $splitter));
  274. return preg_replace_callback('/([A-Z])/', $func, $string);
  275. }
  276. }