• 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
    • Partner With Me
    • Frequently Asked Questions
  • Web Sites
  • Online Marketing
  • WordPress Support
    • Customized WordPress Training
    • 60-for-60 Sessions
  • Web Applications
  • Blog
    • At-A-Glance
    • Blog Articles Grouped by Category
    • Case Studies
    • General
    • Portfolio
    • Reviews
    • Snippets
    • Techniques
    • Projects
  • Contact
    • Purchase Retainer Consulting Hours
    • About Retainer Consulting Hours
    • Book a Meeting with Jeff
    • Tell Me About Your Web Project
    • nSiteful Newsletter Archives
    • nSiteful Rewards

By Jeff - 10/2/2013
Cats: Techniques · Tags: php, regex, Web forms

techniques

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

Like what you see? Share with others and join my mailing list. No long-term commitment, unsubscribe any time.

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

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

  • Post-Specific CSS for WordPress October 5, 2026
  • You built an online course, but they haven’t come. September 16, 2026
  • Custom MemberPress Pricing Pages December 19, 2025
  • CSS Data Attribute Override December 11, 2025
  • MemberPress Rules Using Custom Taxonomies and Custom Roles December 10, 2025

Filter By Category/Tag

Categories

  • Case Studies (8)
  • For Staff (1)
  • General (73)
  • Portfolio (7)
  • Reviews (14)
  • Snippets (24)
  • Techniques (52)

Popular Tags

Advanced Custom Fields Blogging Child Themes Content Marketing CSS Customer Service Custom Fields Custom Post Types Diagnostics Domain Names Facebook FooGallery Genesis Hosting HTML Images iPhone Libra Live Chat Marketing Media MemberPress MemberPress Courses Membership Sites Mobile-Friendly MySQL Photo Gallery php Pinterest Plugins Post Formats Pricing Project Management SEBA SEO Seth Godin Shortcodes Social Networking Surveys Taxonomies Twitter Video Web design Web forms WordPress

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

Book Meeting
770-772-5134
Email Jeff
Send Money
All Ways

Copyright 2026, nSiteful Web Builders, Inc.

Cookies Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}
https://iframe.mediadelivery.net/embed/392008/42d18bc1-2adc-4741-b733-053d08d09c32
https://vz-000c5976-3ab.b-cdn.net/42d18bc1-2adc-4741-b733-053d08d09c32/play_720p.mp4

Receive occasional emails from Jeff