Skip to content

Instantly share code, notes, and snippets.

@13k
Created March 28, 2010 00:09
Show Gist options
  • Select an option

  • Save 13k/346446 to your computer and use it in GitHub Desktop.

Select an option

Save 13k/346446 to your computer and use it in GitHub Desktop.
<?php
/***
* Helper functions to convert strings:
* - from CamelCase to camel_case and;
* - from under_score to UnderScore
*
* These are very probably not fast (or the fastest version).
***/
function camelize($str)
{
return implode('', explode(' ', ucwords(implode(' ', explode('_', $str)))));
}
function underline($str)
{
$words = array();
$split = preg_split('/([A-Z])/', $str, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
for ($i = 0; $i < count($split); $i += 2) {
$words[] = strtolower($split[$i] . $split[$i+1]);
}
return implode('_', $words);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment