0

I am trying to improve search result using PHP. e.g. when product name is ZSX-3185BC user can not find the product when put something like ZSX3185BC. So far i get something like:

$input = '01.10-2010';  
$to_replace = array('.','-');
$clean = str_replace($to_replace, '/',$input);`

I've tried this, but it does not work correctly (not sure, posted in comment without elaboration -ed)

$str = ""; 
$len = strlen($strSearch) - 1; 
// Loop through the search string to check each charecter is acceptable and strip those that are not 
for ($i = 0; $i <= $len; $i++) {
    if(preg_match("/\w|-| /",$strSearch[$i])) {
        $str = $str . $strSearch[$i]; 
    }
}
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
Nasan Ert
  • 41
  • 1
  • 5
  • 1
    would you like to get the search results replacing special charaters directly in mysql query? – Ravinder Singh Jun 27 '12 at 10:16
  • show some exact code what your tried. – Nikson Kanti Paul Jun 27 '12 at 10:20
  • `$str = ""; $len = strlen($strSearch) - 1; // Loop through the search string to check each charecter is acceptable and strip those that are not For ($i = 0; $i <= $len; $i++) { if(preg_match("/\w|-| /",$strSearch[$i])) { $str = $str . $strSearch[$i]; } }` – Nasan Ert Jun 27 '12 at 13:35

2 Answers2

0

You should try to do this kind of fuzzy matching directly in your database query. A common approach to fuzzy string matching is the Levenshtein distance.

MySQL does not have this built in, however, it can be added. See this SO question.

The basic way is to add a stored function to MySQL. There is a Levenshtein implementation as a stored function.

Community
  • 1
  • 1
pixelistik
  • 7,541
  • 3
  • 32
  • 42
-1

Try this :

SELECT * FROM tablename WHERE replace(fieldname, '-', '') like '%ZSX3185BC%';
Ravinder Singh
  • 3,113
  • 6
  • 30
  • 46