Guest User

Untitled

a guest
Jan 12th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. <?php
  2.  
  3. class StringTransform
  4. {
  5. /**
  6. * This Method Converts a String to Title Case
  7. *
  8. * @param string $str
  9. * @param bool $apStyle use AP-Style title case if true
  10. * @return string
  11. */
  12. public static function titleCase($str, $apStyle = false) {
  13. $str = strtolower($str);
  14.  
  15. $exclude = 'the a an of by at on in so to for from and or but like';
  16. if (!$apStyle) $exclude .= ' with before after over';
  17. $exclude = explode(' ', $exclude);
  18.  
  19. $regex = "/(?<!\w)[\p{L}']+/u";
  20.  
  21. preg_match_all($regex, $str, $matches);
  22. $matchCount = count($matches[0]);
  23.  
  24. $wordIdx = 0;
  25. $str = preg_replace_callback($regex, function($matches) use ($exclude, $matchCount, &$wordIdx) {
  26. // capitalize all words not included in $excluded,
  27. // with the first and last word always getting capitalized
  28. if (!in_array($matches[0], $exclude) || ($wordIdx == 0 || $wordIdx == $matchCount - 1)) {
  29. $word = ucfirst($matches[0]);
  30. } else {
  31. $word = $matches[0];
  32. }
  33.  
  34. $wordIdx++;
  35.  
  36. return $word;
  37. }, $str);
  38.  
  39. // capitalize letter after colon and parentheses
  40. $str = preg_replace_callback('/(?<=:\s|\()\p{L}/u', function($matches) {
  41. return strtoupper($matches[0]);
  42. }, $str);
  43.  
  44. return $str;
  45. }
  46.  
  47. /**
  48. * ThisMethodConvertsAStringToPascalCase
  49. *
  50. * @param string $str
  51. * @return string
  52. */
  53. public static function pascalCase($str) {
  54. return self::replaceWhitespace(ucwords($str));
  55. }
  56.  
  57. /**
  58. * thisMethodConvertsAStringToCamelCase
  59. *
  60. * @param string $str
  61. * @return string
  62. */
  63. public static function camelCase($str) {
  64. return lcfirst(self::replaceWhitespace(ucwords($str)));
  65. }
  66.  
  67. /**
  68. * this_method_converts_a_string_to_snake_case
  69. *
  70. * @param string $str
  71. * @return string
  72. */
  73. public static function snakeCase($str) {
  74. return self::replaceWhitespace(strtolower($str), '_');
  75. }
  76.  
  77. /**
  78. * ThIs MeThOd CoNvErTs A sTrInG tO sTuDlYcApS
  79. *
  80. * @param string $str
  81. * @param bool $upperOdd uppercase odd character positions
  82. * @param bool $whitespace if false, whitespaces will be removed
  83. * @return string
  84. */
  85. public static function mixedCase($str, $upperOdd = false, $whitespace = true) {
  86. if (!$whitespace) {
  87. $str = self::replaceWhitespace($str);
  88. }
  89.  
  90. $letters = str_split(strtolower($str));
  91.  
  92. $i = 0;
  93. foreach ($letters as &$letter) {
  94. // only act on letters, ignore whitespace and other chars
  95. if (preg_match('/\p{L}/u', $letter)) {
  96. if ($i % 2 === (int) $upperOdd) {
  97. $letter = strtoupper($letter);
  98. }
  99.  
  100. $i++;
  101. }
  102. }
  103.  
  104. $str = implode($letters);
  105.  
  106. return $str;
  107. }
  108.  
  109. /**
  110. * Th1s m3th0d c0nv3rts 4 str1ng t0 l33t
  111. *
  112. * @param string $str
  113. * @param string|array $mapping 'simple', 'full' (for all leet numbers)
  114. * or associative array for custom mapping, in the form 'letter' => number
  115. * @return string
  116. */
  117. public static function leet($str, $mapping = 'simple') {
  118. $leetChars = array(
  119. 'a' => 4,
  120. 'e' => 3,
  121. 'i' => 1,
  122. 'o' => 0
  123. );
  124.  
  125. if ($mapping == 'full') {
  126. $extended = array(
  127. 'g' => 6,
  128. 's' => 5,
  129. 't' => 7
  130. );
  131.  
  132. $leetChars = array_merge($leetChars, $extended);
  133. } elseif (is_array($mapping)) {
  134. $leetChars = $mapping;
  135. }
  136.  
  137. $letters = str_split($str);
  138.  
  139. foreach ($letters as &$letter) {
  140. if (array_key_exists(strtolower($letter), $leetChars)) {
  141. $letter = strtolower($letter);
  142. $letter = $leetChars[$letter];
  143. }
  144. }
  145.  
  146. $str = implode($letters);
  147.  
  148. return $str;
  149. }
  150.  
  151. private static function replaceWhitespace($str, $separator = '') {
  152. return preg_replace('/\s+/', $separator, $str);
  153. }
  154. }
Add Comment
Please, Sign In to add comment