Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Wednesday, September 16, 2015

[PHP] Spread camel case

My work has me doing Wordpress plugins from time to time. The following snippets comes from one of those projects. It's text a camel-case string and inserts spaces in front of capitals and numbers, turning the item into some more human-readable.
function spreadCamel($txt) 
 {
  $temp = substr($txt,0,1);
  for ($i = 1; $i < strlen($txt); $i++) {
   $c = substr($txt, $i, 1);
   if (ctype_upper($c) || ctype_digit($c)) {
    $temp = $temp . " ";
   }
   $temp = $temp . $c;
  }
  return $temp;
 } 
An example invocation:
echo spreadCamel("MyDogHasFleasAbout1Million");
the result of that being
My Dog Has Fleas About 1 Million
I hope that helps someone. There may be a better/faster/more efficient way of doing it. If there is, please post in the comments.

© Copyright Bruce M. Axtens, 2015

Sunday, December 19, 2010

[Javascript] Wildcard string matching / globbing, Take 2.

Okay, I'm convinced: you just can't beat regular expressions. (Okay, SNOBOL4's pattern matching is streets ahead, but there's nothing out there like that for Javascript, AFAIK.)

I've reworked the matchesWild function, caling it grepWild. It takes the wildCard parameter as before but instead of all the substr stuff, it replaces '?' with '.', '*' with '.*' and wraps with '^' and '$'. Then it feeds that into match().

It's simpler, easier on the eyes, and faster too.
© Bruce M. Axtens, 2010