Main
Latest
- Exams? Commented!
- Coca-Cola: Brrr!
- Gmail now supports IMAP!!!
- MD5 and SHA-1 Password Hashing in PHP / MySQL
- Opposites
- Cape Town - Here we come!
- Microsoft DNS Server
- RFC 3339 Timezones in Joomla! 1.5
- TinyLink, Ajaxified!
- Tinylink API: Consumer PHP Shell Script
Archives
- June 2004
- July 2004
- August 2004
- September 2004
- October 2004
- November 2004
- December 2004
- January 2005
- February 2005
- March 2005
- April 2005
- May 2005
- June 2005
- July 2005
- August 2005
- September 2005
- October 2005
- November 2005
- December 2005
- January 2006
- February 2006
- March 2006
- April 2006
- May 2006
- June 2006
- July 2006
- August 2006
- September 2006
- November 2006
- December 2006
- January 2007
- February 2007
- March 2007
- April 2007
- May 2007
- June 2007
- July 2007
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- May 2008
- June 2008
- July 2008
- August 2008
- September 2008
- October 2008
- November 2008
- December 2008
- January 2009
PHP CamelCase Explode
I just came up with a function that would seem obvious but I didn't find an existing implementation yet. Here goes:
<?php
/**
* Splits up a string into an array similar to the explode() function but according to CamelCase.
* Uppercase characters are treated as the separator but returned as part of the respective array elements.
* @author Charl van Niekerk <charlvn@charlvn.za.net>
* @param string $string The original string
* @param bool $lower Should the uppercase characters be converted to lowercase in the resulting array?
* @return array The given string split up into an array according to the case of the individual characters.
*/
function explodeCase($string, $lower = true)
{
// Initialise the array to be returned
$array = array();
// Initialise a temporary string to hold the current array element before it's pushed onto the end of the array
$segment = '';
// Loop through each character in the string
foreach (str_split($string) as $char) {
// If the current character is uppercase
if (ctype_upper($char)) {
// If the old segment is not empty (for when the original string starts with an uppercase character)
if ($segment) {
// Push the old segment onto the array
$array[] = $segment;
}
// Set the character (either uppercase or lowercase) as the start of the new segment
$segment = $lower ? strtolower($char) : $char;
} else { // If the character is lowercase or special
// Add the character to the end of the current segment
$segment .= $char;
}
}
// If the last segment exists (for when the original string is empty)
if ($segment) {
// Push it onto the array
$array[] = $segment;
}
// Return the resulting array
return $array;
}
?>
Example of use:
<?php print_r(explodeCase('IPwnTehNoobs')); ?>
This will give you the following:
Array
(
[0] => i
[1] => pwn
[2] => teh
[3] => noobs
)
Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.


3 Comments
Comment by
d3th_n1gG4 on Thursday, December 20, 2007 7:15:00 AM
Trust a saffer to find a solution for another saffer :)
tyvm, saved me a bit of unnecessary work this morning
Comment by
d3th_n1gG4 on Thursday, December 20, 2007 7:30:00 AM
Have extended your function a bit, basically it will either return an array or for laziness sake a string, which is then padded via a passed character....
function explodeCase_Ext($string, $lower = true, $asString = true, $stringPadChar = " "){
// Initialise the array to be returned
$array = array();
// Initialise a temporary string to hold the current array element before it's pushed onto the end of the array
$segment = '';
// Loop through each character in the string
foreach (str_split($string) as $char) {
// If the current character is uppercase
if (ctype_upper($char)) {
// If the old segment is not empty (for when the original string starts with an uppercase character)
if ($segment) {
// Push the old segment onto the array
$array[] = $segment;
}
// Set the character (either uppercase or lowercase) as the start of the new segment
$segment = $lower ? strtolower($char) : $char;
} else { // If the character is lowercase or special
// Add the character to the end of the current segment
$segment .= $char;
}
}
// If the last segment exists (for when the original string is empty)
if ($segment) {
// Push it onto the array
$array[] = $segment;
}
// Return the resulting array
if($asString === TRUE){ //Are we required to return this as a string?
$retString = "";
foreach($array as $var){ //Iterate through the array
$retString.= "{$var}{$stringPadChar}"; //Concat the array element with the padding character onto the string
}
return substr($retString, 0, (strlen($retString)-1)); //we will have padded past the last character, so we return the entire string - 1 char to eliminate this
}
else{
return $array;
}
}
Comment by
Charl van Niekerk on Friday, December 21, 2007 2:02:00 PM
I'm definitely not a saffer, but I do believe in reusability. Thanks for the script!
Post a Comment