Last active
October 5, 2018 11:04
-
-
Save d4rkne55/7a0a24860634fb8eddc6c965c3b88a1a to your computer and use it in GitHub Desktop.
Revisions
-
d4rkne55 revised this gist
Oct 5, 2018 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -85,6 +85,7 @@ public static function snakeCase($str) { /** * ThIs MeThOd CoNvErTs A sTrInG tO sTuDlYcApS * multibyte-safe * * @param string $str * @param bool $upperOdd uppercase odd character positions @@ -96,14 +97,14 @@ public static function mixedCase($str, $upperOdd = false, $whitespace = true) { $str = self::replaceWhitespace($str); } $letters = preg_split('//u', mb_strtolower($str), null, PREG_SPLIT_NO_EMPTY); $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 = mb_strtoupper($letter); } $i++; @@ -117,6 +118,7 @@ public static function mixedCase($str, $upperOdd = false, $whitespace = true) { /** * Th1s m3th0d c0nv3rts 4 str1ng t0 l33t * multibyte-safe * * @param string $str * @param string|array $mapping 'simple', 'full' (for all leet numbers) -
d4rkne55 revised this gist
Oct 5, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ class StringTransform { /** * This Method Converts a String to Title Case * Not context-aware and just for english. * -
d4rkne55 revised this gist
Oct 5, 2018 . 1 changed file with 20 additions and 22 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ class StringTransform { /** * This Method Converts a String to Title Case * Not context-aware and just for english. * @@ -28,31 +28,29 @@ public static function titleCase($str, $apStyle = false) { $regex = "/(?<!\w)[\p{L}']+/u"; // split subphrases $substr = preg_split('/(\s?[\(\)\/:!?.]\s?)/', $str, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); for ($i = 0; $i < count($substr); $i++) { preg_match_all($regex, $substr[$i], $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $match) { list($word, $offset) = $match; $isFirst = ($offset == 0); $isLast = ($offset == strlen(rtrim($substr[$i])) - strlen($word)); // capitalize all words not included in $excluded, // except shortened words with quote (eg. 'til), // with the first and last word always getting capitalized if ((!in_array($word, $exclude) && $word[0] != "'") || ($isFirst || $isLast)) { $word = ucfirst($word); } $substr[$i] = substr_replace($substr[$i], $word, $offset, strlen($word)); } } return implode($substr); } /** -
d4rkne55 revised this gist
Sep 30, 2018 . 1 changed file with 16 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -4,16 +4,26 @@ class StringTransform { /** * This Method Converts a String to Title Case * Not context-aware and just for english. * * @param string $str * @param bool $apStyle use AP-Style title case if true * otherwise MLA-based (default) * @return string */ public static function titleCase($str, $apStyle = false) { $str = strtolower($str); // normalize quotes $str = strtr($str, array( "‘" => "'", "’" => "'", '“' => '"', '”' => '"' )); $exclude = 'the a an of by at on in so to for and or nor but yet'; if (!$apStyle) $exclude .= ' as than from into like with within since until upon before after over under up down above near between beyond'; $exclude = explode(' ', $exclude); $regex = "/(?<!\w)[\p{L}']+/u"; @@ -24,8 +34,9 @@ public static function titleCase($str, $apStyle = false) { $wordIdx = 0; $str = preg_replace_callback($regex, function($matches) use ($exclude, $matchCount, &$wordIdx) { // capitalize all words not included in $excluded, // except shortened words with quote (eg. 'til), // with the first and last word always getting capitalized if ((!in_array($matches[0], $exclude) && $matches[0][0] != "'") || ($wordIdx == 0 || $wordIdx == $matchCount - 1)) { $word = ucfirst($matches[0]); } else { $word = $matches[0]; @@ -36,8 +47,8 @@ public static function titleCase($str, $apStyle = false) { return $word; }, $str); // capitalize letter after colon, slash and parentheses $str = preg_replace_callback('/(?<=:\s|\/\s|\()\p{L}/u', function($matches) { return strtoupper($matches[0]); }, $str); -
d4rkne55 created this gist
Jan 5, 2018 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,154 @@ <?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); } }