PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come analizzare la stringa di ricerca dell'utente per la query Postgresql?

Prova questo:

$a=array('word1 +word2','word1+word2','word1    -word2',' word1-word2','word1 word2','word1  word2');

foreach ($a as &$v) {
  $v=preg_replace('/ +/','|',        // last: change blanks to |
     preg_replace('/ *(?=[!&])/','', // delete blanks before ! or &
     strtr(trim($v),array('-'=>'&!','+'=>'&'))  // turn + and - into & and !&
     ));
}
print_r($a);

Questo darà:

Array
(
    [0] => word1&word2
    [1] => word1&word2
    [2] => word1&!word2
    [3] => word1&!word2
    [4] => word1|word2
    [5] => word1|word2
)