tag:blogger.com,1999:blog-6950106.post-71120357099028676042007-11-03T00:46:00.000Z2007-11-03T13:21:09.587Z2007-11-03T13:21:09.587ZPHP CamelCase Explode 2.0<p>In an update to yesterday's post <a href="http://blog.charlvn.za.net/2007/11/php-camelcase-explode.html">PHP CamelCase Explode</a>, here is a different implementation making use of regular expressions:</p>
<pre><code><?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)
{
// Split up the string into an array according to the uppercase characters
$array = preg_split('/([A-Z][^A-Z]*)/', $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
// Convert all the array elements to lowercase if desired
if ($lower) {
$array = array_map(strtolower, $array);
}
// Return the resulting array
return $array;
}
?></code></pre>
<p>This should do the same as the other algorithm but just using less code.</p>
<p>Don't you just "love" the title? 2.0 is the new green.</p>
<p>Oh yes, and when I mention <q>yesterday</q>, I mean before I went to bed. It's technically speaking still the same day both in South African and <abbr title="Universal Coordinated Time">UTC</abbr> terms. There's only two hours of difference anyway.</p>Charl van Niekerkhttp://www.blogger.com/profile/16555795103153299929noreply@blogger.com