Last active
October 5, 2018 11:04
-
-
Save d4rkne55/7a0a24860634fb8eddc6c965c3b88a1a to your computer and use it in GitHub Desktop.
Class for some transformations on Strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class StringTransform | |
| { | |
| /** | |
| * This Method Converts a String to Title Case | |
| * | |
| * @param string $str | |
| * @param bool $apStyle use AP-Style title case if true | |
| * @return string | |
| */ | |
| public static function titleCase($str, $apStyle = false) { | |
| $str = strtolower($str); | |
| $exclude = 'the a an of by at on in so to for from and or but like'; | |
| if (!$apStyle) $exclude .= ' with before after over'; | |
| $exclude = explode(' ', $exclude); | |
| $regex = "/(?<!\w)[\p{L}']+/u"; | |
| preg_match_all($regex, $str, $matches); | |
| $matchCount = count($matches[0]); | |
| $wordIdx = 0; | |
| $str = preg_replace_callback($regex, function($matches) use ($exclude, $matchCount, &$wordIdx) { | |
| // capitalize all words not included in $excluded, | |
| // with the first and last word always getting capitalized | |
| if (!in_array($matches[0], $exclude) || ($wordIdx == 0 || $wordIdx == $matchCount - 1)) { | |
| $word = ucfirst($matches[0]); | |
| } else { | |
| $word = $matches[0]; | |
| } | |
| $wordIdx++; | |
| return $word; | |
| }, $str); | |
| // capitalize letter after colon and parentheses | |
| $str = preg_replace_callback('/(?<=:\s|\()\p{L}/u', function($matches) { | |
| return strtoupper($matches[0]); | |
| }, $str); | |
| return $str; | |
| } | |
| /** | |
| * ThisMethodConvertsAStringToPascalCase | |
| * | |
| * @param string $str | |
| * @return string | |
| */ | |
| public static function pascalCase($str) { | |
| return self::replaceWhitespace(ucwords($str)); | |
| } | |
| /** | |
| * thisMethodConvertsAStringToCamelCase | |
| * | |
| * @param string $str | |
| * @return string | |
| */ | |
| public static function camelCase($str) { | |
| return lcfirst(self::replaceWhitespace(ucwords($str))); | |
| } | |
| /** | |
| * this_method_converts_a_string_to_snake_case | |
| * | |
| * @param string $str | |
| * @return string | |
| */ | |
| public static function snakeCase($str) { | |
| return self::replaceWhitespace(strtolower($str), '_'); | |
| } | |
| /** | |
| * ThIs MeThOd CoNvErTs A sTrInG tO sTuDlYcApS | |
| * | |
| * @param string $str | |
| * @param bool $upperOdd uppercase odd character positions | |
| * @param bool $whitespace if false, whitespaces will be removed | |
| * @return string | |
| */ | |
| public static function mixedCase($str, $upperOdd = false, $whitespace = true) { | |
| if (!$whitespace) { | |
| $str = self::replaceWhitespace($str); | |
| } | |
| $letters = str_split(strtolower($str)); | |
| $i = 0; | |
| foreach ($letters as &$letter) { | |
| // only act on letters, ignore whitespace and other chars | |
| if (preg_match('/\p{L}/u', $letter)) { | |
| if ($i % 2 === (int) $upperOdd) { | |
| $letter = strtoupper($letter); | |
| } | |
| $i++; | |
| } | |
| } | |
| $str = implode($letters); | |
| return $str; | |
| } | |
| /** | |
| * Th1s m3th0d c0nv3rts 4 str1ng t0 l33t | |
| * | |
| * @param string $str | |
| * @param string|array $mapping 'simple', 'full' (for all leet numbers) | |
| * or associative array for custom mapping, in the form 'letter' => number | |
| * @return string | |
| */ | |
| public static function leet($str, $mapping = 'simple') { | |
| $leetChars = array( | |
| 'a' => 4, | |
| 'e' => 3, | |
| 'i' => 1, | |
| 'o' => 0 | |
| ); | |
| if ($mapping == 'full') { | |
| $extended = array( | |
| 'g' => 6, | |
| 's' => 5, | |
| 't' => 7 | |
| ); | |
| $leetChars = array_merge($leetChars, $extended); | |
| } elseif (is_array($mapping)) { | |
| $leetChars = $mapping; | |
| } | |
| $letters = str_split($str); | |
| foreach ($letters as &$letter) { | |
| if (array_key_exists(strtolower($letter), $leetChars)) { | |
| $letter = strtolower($letter); | |
| $letter = $leetChars[$letter]; | |
| } | |
| } | |
| $str = implode($letters); | |
| return $str; | |
| } | |
| private static function replaceWhitespace($str, $separator = '') { | |
| return preg_replace('/\s+/', $separator, $str); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment