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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -