php - Search MySQL database with regex substitutions -
i want make product search engine user types in product code , bring result, easy.
but, want able compensate numbers letters , vice versa.
e.g user types 6o12l, product code 60121.
what need put in sql query bring products 6o12l and/or 60121?
so far have isn't working, keeps bringing same result everytime no matter type in:
$searchstring = $_post['query'] ; $searchstring = preg_replace('#\w#', '', $searchstring); $firstletter = substr($searchstring, 0, 1) ; include("db.php") ; $result = $dbh->prepare("select productcode products productcoderegexp '6[o0]12[1l]' , productcode '$firstletter%'") ; $result->execute() ; while($row = $result->fetch(pdo::fetch_assoc)) { echo $row['productcode'].'<br />' ; }
i have managed working, have encountered new problem.
i'm using str_replace substitute letters numbers , viceversa in users query string, work 1 or other, not both:
$qstring = str_replace(array('o', 'l', '0', '1'), array('[o0]', '[1l]', '[o0]', '[1l]'), $searchstring) ;
which gives me mangled output of e.g. a[[1l]l]bc instead of a[1l]bc
do have product codes letters? can translate query string numbers before run query. that's easiest thing do, , faster testing both.
Comments
Post a Comment