Last active
December 21, 2015 11:59
-
-
Save andrewspear/6302971 to your computer and use it in GitHub Desktop.
Convert a camelCase or underscore_separated string to Title Case.
Useful for converting database column names to readable text.
This file contains 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
<? | |
// Convert a camel-case or underscore_separated string to Title Case | |
function valueToTitle($string) { | |
$string = preg_replace('/(?!^)([[:upper:]][[:lower:]]+)/', ' $0', $string); | |
$string = preg_replace('/(?!^)([[:lower:]])([[:upper:]])/', '$1 $2', $string); | |
$string = str_replace('_', ' ', $string); | |
$string = ucwords($string); | |
return $string; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If all you need is camelCase to Title Case conversion then there's a single line function for that here: https://gist.github.com/andrewspear/6680029