php - How to parse user search string for Postgresql query? -
i created search engine on site using full text search in postegresql. added searchbox in php page in users can write strings these:
word1 +word2 word1+word2 word1 -word2 word1-word2 word1 word2 word1 word2
how convert them following strings?
word1&word2 word1&word2 word1&!word2 word1&!word2 word1|word2 word1|word2
i tried several solutions none works cases. last 1 tried following:
$user_query_string = trim($_get['search']); $final_query_string = str_replace(array('+', ' ', '-'), array('&','|', '&!'), $user_query_string);
try this:
$a=array('word1 +word2','word1+word2','word1 -word2',' word1-word2','word1 word2','word1 word2'); foreach ($a &$v) { $v=preg_replace('/ +/','|', // last: change blanks | preg_replace('/ *(?=[!&])/','', // delete blanks before ! or & strtr(trim($v),array('-'=>'&!','+'=>'&')) // turn + , - & , !& )); } print_r($a);
this give:
array ( [0] => word1&word2 [1] => word1&word2 [2] => word1&!word2 [3] => word1&!word2 [4] => word1|word2 [5] => word1|word2 )
Comments
Post a Comment