Skip to content

Instantly share code, notes, and snippets.

@andrewspear
Last active December 21, 2015 11:59
Show Gist options
  • Save andrewspear/6302971 to your computer and use it in GitHub Desktop.
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.
<?
// 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;
}
?>
@andrewspear
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment