Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Class for some transformations on Strings
<?php
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";
// 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);
}
/**
* 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
* multibyte-safe
*
* @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 = 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++;
}
}
$str = implode($letters);
return $str;
}
/**
* Th1s m3th0d c0nv3rts 4 str1ng t0 l33t
* multibyte-safe
*
* @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
You can’t perform that action at this time.