-
-
Save carlosleonam/2c44e49279d41ef056d495885a7282fd to your computer and use it in GitHub Desktop.
PHP Camel Case functions
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 | |
// source: http://www.paulferrett.com/2009/php-camel-case-functions/ | |
/** | |
* Translates a camel case string into a string with underscores (e.g. firstName -> first_name) | |
* @param string $str String in camel case format | |
* @return string $str Translated into underscore format | |
*/ | |
function from_camel_case($str) { | |
$str[0] = strtolower($str[0]); | |
$func = create_function('$c', 'return "_" . strtolower($c[1]);'); | |
return preg_replace_callback('/([A-Z])/', $func, $str); | |
} | |
/** | |
* Translates a string with underscores into camel case (e.g. first_name -> firstName) | |
* @param string $str String in underscore format | |
* @param bool $capitalise_first_char If true, capitalise the first char in $str | |
* @return string $str translated into camel caps | |
*/ | |
function to_camel_case($str, $capitalise_first_char = false) { | |
if($capitalise_first_char) { | |
$str[0] = strtoupper($str[0]); | |
} | |
$func = create_function('$c', 'return strtoupper($c[1]);'); | |
return preg_replace_callback('/_([a-z])/', $func, $str); | |
} | |
function toCamelCase($word) { | |
return lcfirst(str_replace(‘ ‘, ”, ucwords(strtr($word, ‘_-’, ‘ ‘)))); | |
} | |
public static function fromCamelCase($str) | |
{ | |
$str[0] = strtolower($str[0]); | |
return preg_replace('/([A-Z])/e', "'_' . strtolower('\\1')", $str); | |
} | |
public static function toCamelCase($str, $capitaliseFirstChar = false) | |
{ | |
if ($capitaliseFirstChar) { | |
$str[0] = strtoupper($str[0]); | |
} | |
return preg_replace('/_([a-z])/e', "strtoupper('\\1')", $str); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment