I’m currently making a script that searches a table for the term(s) a user entered. My current idea is outlined below but I’m not sure it’s very efficient:
1) preg_replace out any words under 3 characters long and some common words like ‘they’re’ etc. unless they’re preceeded by a ‘+’.
2) Preg’ < and > to < and > and escape the characters that need escaping. Strip off punctuation.
3) If $term has ” at the start and end, Preg off ^“ and “$ and define $search0 as $term else define $words = explode(” “, $term); and define $search1 as ‘%$term%’.
4) $to_echo = returned rows from $search0
5) $to_echo .= returned rows from $search1
6) if($words) { $words2 = “%”; foreach($words as $w) { $words2 .= $w . “%” } }
7) $to_echo .= returned rows from a mysql_query() SELECTing $words2
8) echo($to_echo);
I hope that made any sense – I would obviously add some error checking too – “No terms entered”, “No results found” etc.
Basically, what I need is some feedback on whether there is a more efficient way of doing it and if I’ve missed anything.
Thanks & Regards, Jonnie