Regular expression example for search terms Aug15 '05
On a corporate site that I am working on, I have included a search box, that searches through products.
Users can search by entering a product title, or a product inventory id. The ability to search by inventory id is a feature mainly directed at internal staff. Most customers won’t need to search by id.
Single text box
For the search form, I’ve only included a single text field.
At first, I was going to include a radio button, with the option to search by name, or by id. The user would "activate" whichever type of search they wanted to conduct, by clicking the appropriate radio button, and then enter their search term in the box.
However, I wanted the form to be as simple as possible. And, asking users to click the appropriate button before they search, is asking too much. I didn’t want a mis-labeled search to turn up zero results, simply because the user forgot to click the proper radio button.
With the web, you have to assume that every user is completely lazy and incompetent. After all, I am a user too, and I am extremely lazy and incompetent, when I want information. I want the easiest web site possible, with as few clicks as possible.
In any case, getting rid of the radio button option presented me with a small problem.
The product title and product id are in two separate database fields.
It was quite difficult to come up with a way to search a FULLTEXT field, and an INT field, in MySQL - at the same time. By this, I mean - with a single query.
So, what I ended up doing was writing a regular expression, to determine what type of search request was made, and then based on that - the proper field is queried.
Numbers or no numbers?
Since all of our product id’s contain a number somewhere in them, I decided to use this as criteria.
So, in plain English, here’s how the following regular expression works.
If the ‘search term that the user submits’ does contain a number, then we proceed to search the product id field. Otherwise, we search the product title field.
It’s that simple:
if ( eregi("[0-9]", $searchterm) )
This is, of course, in PHP, and the only thing to note is the function eregi, which searches a string ($searchterm) for a pattern ([0-9]).
So, if there is a number in the search term, then we proceed to search the id field. If there is no number present, we search the title field.
Now... the form can contain just a text field, with no radio buttons:
Categories: PHP
, Search
, SQL
, Tutorials ![]()
Add Feedback (view all)
Leave feedback
matthom
is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago.
Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.
Similar Entries
- Firefox keyword search: Google glossary definition (36 recent visits)
- Firefox 3 smart address bar: wildcard search (2988 recent visits)
- Yahoo! Web Services: Image Search (5 recent visits)
- MySQL search criteria - column alias (1371 recent visits)
- Google Docs dynamic search shortcoming (25 recent visits)
- How far to a MapQuest built–in search? (6 recent visits)
Stats
5 unique visits since November 2008
Recent Referrers (click)
- search
- rude expression example
- http://matthom.com/archive/200
- http://matthom.com/archive/200
- search
- search
- rude expression example
- http://matthom.com/archive/200
- http://matthom.com/archive/200
- search
- search
- rude expression example
- http://matthom.com/archive/200
- http://matthom.com/archive/200
- search
- search
- rude expression example