Biting the bullet

Share this!

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

shutter-artisticI 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

cap-php-string-functionsI’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)

cap-php-pcre-functionsThe 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);
}
Share this!

About JeffCohan.com

The nSiteful Tech Blog (the official blog of nSiteful Web Builders, Inc. since January of 2013) is where I (Jeff Cohan) and (occasionally) associates will be posting articles of potential interest to like-minded techies, nSiteful clients who are playing active roles in the maintenance of their own Web sites and blogs, and pretty much anyone interested in how Web strategies and tools can help them reach their goals.

This entry was posted in Techniques and tagged , , by Jeff Cohan. Bookmark the permalink.

About Jeff Cohan

Jeff and his wife, Margie, are the proud parents of Sarah and Jake. Jeff is the founder, president and chief cook and bottle washer of nSiteful Web Builders, Inc., a Web development and Internet Consulting firm. In his spare time, Jeff builds Web sites and Web applications, plays guitar, putters around in his basement woodworking shop, mercilessly spoils his grandchildren, and creates videos from more than two decades of home movies. His current video project is an extended montage of people (mainly family members) asking him to stop filming them.

Leave a Reply

Your email address will not be published. Required fields are marked *