Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

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
)

3 Comments

Comment by Blogger 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 Blogger 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 Blogger 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

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.