Quantcast
Channel: 3magine
Viewing all articles
Browse latest Browse all 9

Camel Case Spaces

$
0
0

Web development is all about having fun, so let’s have some fun with camel case strings.

function camelCaseSpaces($s){
    return trim(preg_replace("/[A-Z]/",' ${0}',$s));
}

Using this custom function you can now convert strings such as “iAmSuperCamelCaseString” to “i Am Super Camel Case String”. One day while working on a CodeIgniter project, I needed a quick access to list links to all available methods in one specific controller where most of the methods where in a camelCase format.  To achieve my goal I came up with the above function and the following sneaky code within the “index” method of a controller named “Buildout”:

//Get all method names:
$m=get_class_methods($this);

//Unset the ones I do not need:
unset(
$m[array_search('index',$m)],
$m[array_search('__construct',$m)],
$m[array_search('get_instance',$m)]
);

//Print all links to my methods:
foreach($m as $n)
   print '<href="/buildout/'.$n.'">'.ucwords(camelCaseSpaces($n)).'</a><br />';

Thanks for reading and feel free to re-use this neat trick.


Viewing all articles
Browse latest Browse all 9

Trending Articles