• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

nSiteful Web Builders

Building a Better Web - One Site at a Time.

  • Home
  • About
    • Testimonials
    • Resources
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
    • 60-for-60 Sessions
  • Web Applications
  • Blog
    • Archive Listing Minimalistic
    • Blog Articles Grouped by Category
    • Case Studies
    • General
    • Portfolio
    • Reviews
    • Snippets
    • Techniques
  • Contact Jeff
    • Purchase Retainer Consulting Hours
    • About Retainer Consulting Hours

By Jeff Cohan, October 2, 2013

Biting the bullet

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

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);
}

Related Posts

  1. Why, When, and How to use sprintf and printf
  2. Case Study: Emergency Notice Banner for Hybrid Web site
  3. How to use Advanced Custom Fields with Genesis
  4. Display WordPress and WooCommerce Tags as Lists
  5. NextGen Gallery Compact Album Mods
  • Choose the best match.

Written by Jeff Cohan · Categorized: Techniques · Tagged: php, regex, Web forms

  • Choose the best match.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

mailchimp signup

Subscribe to get notified when new articles are published. Unsubscribe any time. No spam. I promise. Check out my newsletter archives.

social

Twitter Facebook LinkedIn

Recent Articles

  • Use Case for Custom Post Type: “In The News” March 10, 2023
  • Create a Custom Shortcode to Display a MemberPress Membership Price ANYWHERE on Your Website February 5, 2023
  • Avoid Direct Styling; Use CSS Instead September 21, 2022
  • Blog Tags: What They Are (and What They’re Not) August 5, 2022
  • How to Create a Simple Custom Events Plugin May 24, 2022

Filter By Category/Tag

Categories

  • Case Studies (7)
  • General (61)
  • Portfolio (5)
  • Reviews (12)
  • Snippets (16)
  • Techniques (38)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS CSS Grid Customer Service Custom Fields Custom Post Types Diagnostics Facebook FooGallery Genesis Gutenberg HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses mu-plugins MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management Security SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Trello Twitter Video Web design Web forms WordPress

siteground wp hosting

Web Hosting

wp101

EasyWordPresstutorialvideosforbeginners.
MemberPress CTA

Footer

Background

Web Sites | WordPress Support | Web Applications.

Formally trained in liberal arts and education (I have a B.A. in Government from Harvard and studied Secondary Education at Rutgers Graduate School), I have honed my skills in the communication arts and sciences as a teacher, trainer, instructional designer, writer, photographer, calligrapher, helpdesk manager, database programmer, and multimedia developer.

(I've also been a group counselor, waiter, bartender, bicycle messenger boy, computer salesman, carpenter's helper, financial analyst, and school board president.)

Tech

Systems since 1983.
Web sites since 1994.
PHP since 2001.
WordPress since 2007.

Contact

770-772-5134
Email Jeff
Send Money
All Ways

Copyright 2023, nSiteful Web Builders, Inc.

 

Subscribe

Pardon the interruption. I know popups can be annoying. But I’d love to have you as a subscriber.

Sign up to be notified when new articles are published. Unsubscribe any time.

* indicates required

Powered by MailChimp

×