CamelCase Explode
Sometimes we need a PHP function to split strings like CamelCaseExplode into segments Camel, Case, Explode.
The first thing I found was on Charl van Niekerk's Blog: "PHP CamelCase Explode 2.0". Thanks a lot! The code (a bit minified) looks like this:
/**
* original author: Charl van Niekerk, http://blog.charlvn.za.net/2007/11/php-camelcase-explode-20.html
*/
function camelCaseExplode($string, $lowercase = true) {
$array = preg_split('/([A-Z][^A-Z]*)/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
if ($lowercase) array_map('strtolower', $array);
return $array;
}
More talking happened in the http://us2.php.net/str_split discussions.
Now the big question with any CamelCase explode solution is what it does with uppercase clusters with "MoreThanONECharacterUppercase", or acronyms such as "DOM". This is what different regular expressions do with "MyXMLParsingEngine":
/([A-Z][^A-Z]*)/ gives "My X M L Parsing Engine" /([A-Z][^A-Z]+)/ gives "My XML Parsing Engine" /([A-Z]+[^A-Z]*)/ gives "My XMLParsing Engine"
Now, how can we make a function doing all of this, with a nice interface? We don't want to pass a nasty regexp as a parameter! And we don't want arbitrary-sounding flags, which need to be looked up in a manual.
The solution is: We use exploded example strings as a configuration parameter!
function camelCaseExplode($string, $lowercase = true, $example_string = 'AA Bc', $glue = false)
{
static $regexp_available = array(
'/([A-Z][^A-Z]*)/',
'/([A-Z][^A-Z]+)/',
'/([A-Z]+[^A-Z]*)/',
);
static $regexp_by_example = array();
if (!isset($regexp_by_example[$example_string])) {
$example_array = explode(' ', $example_string);
foreach ($regexp_available as $regexp) {
if (implode(' ', preg_split(
$regexp,
str_replace(' ', '', $example_string),
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
)) == $example_string) {
break;
}
}
$regexp_by_example[$example_string] = $regexp;
}
$array = preg_split(
$regexp_by_example[$example_string],
$string,
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
if ($lowercase) $array = array_map('strtolower', $array);
return is_string($glue) ? implode($glue, $array) : $array;
}
How to use this?
echo '<br>'.camelCaseExplode('ThreeTASTYBurgers', true, 'ABCd', ' ');
echo '<br>'.camelCaseExplode('ThreeTASTYBurgers', true, 'AB Cd', ' ');
echo '<br>'.camelCaseExplode('ThreeTASTYBurgers', true, 'A B Cd', ' ');
echo '<br>'.camelCaseExplode('ThreeTASTYBurgers', false, 'AB Cd', ', ');
gives
ThreeTASTYBurgers Three TASTY Burgers Three T A S T Y Burgers three, tasty, burgers
Bingo!
