Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
- }
- ?>
Add Comment
Please, Sign In to add comment