Last updated February 11th, 2014 at 06:19 am
Last night I bit the bullet. I stepped out of my comfort zone to try a new (for me) kind of solution to a common work problem.
The Situation
I was working on an online order form for a manufacturer of wooden shutters. Several of the form fields accept measurements in inches and fractions of an inch. By "several" I mean 30.
My client’s customers (people in the building trades) have been submitting orders using paper and Excel versions of the form for years, and they are accustomed to entering such measurements like this:
14 5/8
20 15/16
11 1/2
36
So we decided I’d build these form fields as free-form text fields.
You don’t have to be a programmer to understand the validation challenge this decision created. You know what free-form fields are. Anything goes. But anything cannot go when it comes time for the order to be submitted.
So I needed to write code that accepts only valid measurement entries and throws an error otherwise.
My Options
PHP String Functions
I’ve been working in PHP for 12 years. I know the language pretty well, if I do say so myself. PHP is my comfort zone.
PHP has a number of powerful string functions that can parse and evaluate and manipulate free-form text, and I’ve used these many, many times over the years in situations similar to the present one. I was supremely confident that I could code a series of custom PHP functions, utilizing the built-in string functions, to accomplish my objective.
But I couldn’t escape a nagging thought. I couldn’t ignore the fact that there’s another language out there — a language that was created for exactly the kind of challenge these free-form measurement fields posed, a language I’ve played with and even used a little over the last few years, a language I’ve resolved several times to master but haven’t yet.
That language is called…
Regular Expressions (RegEx)
The vocabulary and grammar of RegEx are nothing like those of any programming language I’ve ever used before. I’m not afraid to admit I have found the language intimidating. And because I’ve been successful in using PHP to accomplish many of the things for which RegEx is well-suited (actually, better-suited), I have usually — in the interest of exigency — opted to postpone my RegEx education to another time and go the PHP route.
What I Did
Fight the temptation!
As I started designing the solution for the free-form measurement fields last night, I initially fought the temptation to stay in my PHP comfort zone, and I set out to solve the challenge with regular expressions.
But after a couple of initial frustrating attempts with RegEx, I once again found myself at that familiar crossroads: I could either crack out the books (Mastering Regular Expressions by Jeffrey E.F. Friedl is the seminal work) and revisit my bookmarked online tutorials or I could return to my comfort zone.
Oh well…
I returned to my comfort zone.
I started coding in PHP using those string functions. I wasn’t only in my comfort zone, but I was in the zone. My fingers were flying. My brain was working.
The good news is that within an hour, I had created a chain of six PHP scripts, each performing its own dedicated task leading to the goal of assessing whether a form field entry like 12 15/16 is a valid measurement entry. And I wasn’t far from done.
The bad news is the same thing.
Six functions? And I wasn’t even done yet?
Return to RegEx
So I bit the bullet and returned to RegEx. And am I glad I did. I won’t lie and say it was a snap. But it took much less time than I had anticipated. And the result was a 2-line function that works!
Last night I bit the bullet and stepped out of my comfort zone. It turns out the bullet wasn’t so hard. And it didn’t taste bad at all. In fact, it tasted pretty good.
And now my comfort zone is a little bit bigger.
Postscript
This isn’t a tutorial in regular expressions. But it occurred to me that some of you might be interested in seeing (and perhaps telling me how I could improve on) that 2-line function. So, here it is:
function inch_test( $entry ) { $pattern = "!^ ( \d{1,3} ){1} ( (\s){1} ( 1/2 | 1/4 | 1/8 | 1/16 | 3/4 | 3/8 | 3/16 | 5/8 | 5/16 | 7/8 | 7/16 | 9/16 | 11/16 | 13/16 | 15/16 ) )?$!x"; return preg_match_all($pattern, $entry, $matches, PREG_PATTERN_ORDER); }
Leave a Reply