
11-25-09, 12:28 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
Quote:
Originally Posted by wirehopper
Good point.
In that case, the regular expression needs to allow the entered string to be more tolerant.
Thus - if you modify the regular expression to allow characters between those entered, it should match.
This code is for phone numbers, but it may be helpful here, too. Instead of replacing all the digits in the string, you could replace every character.
PHP Code:
<?php /* A list of phone numbers to test for matches */ $aPhoneNumbers=array( '7085556232', '1(708)555-6232', '(708)555-6232', '708.555.6232', '1.708.555.6232', '1.708.555.6232 ext. 123', '17085556232', '7215556232', '1(721)555-6232', '(721)555-6232', '721.555.6232', '1.721.555.6232', '17215556232' ); /* sPhone is the phone number */ $sPhone='7085556232';
/* Pattern is all digits */ $rPattern='/(\d)/';
/* Replace all non-digits */ $rReplace='\D*${1}';
/* Create the regular expression */ $sRegExp='/'.preg_replace($rPattern,$rReplace,$sPhone).'/'; /* Display it */ echo 'Regular Expression: '.$sRegExp."\n"; /* Run the test on all the input */ foreach ($aPhoneNumbers as $k => $v) check($sRegExp,$v); /* Test function */\ function check($sRegExp,$sPhone) { echo '$sPhone: '.$sPhone.' ? '.preg_match($sRegExp,$sPhone)."\n"; } ?>
Although I used PHP to develop the regexp logic, I was able to convert it to use the MySQL equivalents - like :digit:, etc.
Output
The .? indicates any single character. You may want to adjust that to [.*]? - meaning any string of characters, or something more specific.
Output doesn't match the code - but - the general idea is to intersperse the target characters with others such that it will match.
|
In the above code $v would represent the value from the model column in the database table.
So how do you plan to insert the value from the model column into your check() function?
You still haven't showed us how you used it in the SELECT query.
__________________
Jerry Broughton
|