QA Graphic

Spilt Camel Case Snippet

Have you ever encountered a text, such as 'TheSenateReportDocument' or 'LasVegasHotelsReservations' and wanted an easy way to split and display the text. It's very easy to setup a TextExpander snippet to display the text with the proper spaces. To get this done, we'll need a tiny PHP function with preg_replace.

If your not familiar with this text format here's a bit of information from Wikipedia:

CamelCase (also camel caps or medial capitals) is the practice of writing compound words or phrases such that each word or abbreviation begins with a capital letter. Camel case may start with a capital or, especially in programming languages, with a lowercase letter. Common examples are LibreOffice, PowerPoint, iPhone, or in online usernames such as "JohnSmith".

Here's what my TextExpander code looks like:

CamelCase2.jpg

You can simply copy/paste this code:


#!/usr/bin/php

echo splitByCaps('%clipboard');
function splitByCaps($string)
{
return preg_replace('/([a-z0-9])?([A-Z])/','$1 $2',$string);}
?>

I assigned 's.text' as my Abbreviation. Be creative and assign something that you'll remember and not likely mistype.

Now I simply have to copy the text into the clipboard and type in the abbreviation to display the text correctly. In most situations, simply double clicking on the word will select it. So, for example, the TheSenateReportDocument, now become The Senate Report Document.

I find this snippet comes in handy when I have image filenames with CamelCase and I want to insert the proper ALT tag.

 

About

I find TextExpander, by Smile on My Mac, to be an awesome productivity tool. This past winter, I posted some cool snippets that demonstrate the power of the application. Occasionally, I will post some new snippets that I think will be useful in anyone's TextExpander library.

Schedule

SaturdayInternet Tools
SundayOpen Topic
MondayMedia Monday
TuesdayQA
WednesdayAffinity
ThursdayBBEdit
FridayMacintosh