本文介绍了如何解析Postgresql查询的用户搜索字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Postegresql中的全文搜索在我的网站上创建了一个搜索引擎。
i在我的PHP页面中添加了一个搜索框,用户可以在其中编写这样的字符串:
word1 + word2
word1 + word2
word1 -word2
word1-word2
word1 word2
word1 word2
如何将它们转换为下列字符串?
word1& word2
word1& amp; ; word2
word1&!word2
word1&!word2
word1 | word2
word1 | word2
我尝试了几种解决方案,但没有一个适用于所有情况。
我试过的最后一个是:
$ b $ pre $ $ user_query_string = trim($ _ GET ['search']) ;
$ final_query_string = str_replace(array('+','',' - '),array('&','|','&!'),$ user_query_string);
解决方案
试试这个:
$ a = array('word1 + word2','word1 + word2','word1 -word2','word1-word2','word1 word2', 'word1 word2');
$ b foreach($ a as& $ v){
$ v = preg_replace('/ + /','|',// last:将空格更改为|
preg_replace('/ *(?= [!&])/','',//删除!或&
strtr(trim($ v),array(' - '=>' &!','+'=>'&'))//将+和 - 转换为&和!&
));
}
print_r($ a);
这会给出:
数组
(
[0] => word1& word2
[1] => word1& word2
[2] => word1& amp ; word2
[3] => word1&!word2
[4] => word1 | word2
[5] => word1 | word2
)
i created a search engine on my site using full text search in Postegresql.i added a searchbox in my PHP page in which users can write strings like these:
word1 +word2
word1+word2
word1 -word2
word1-word2
word1 word2
word1 word2
how to convert them to the following strings?
word1&word2
word1&word2
word1&!word2
word1&!word2
word1|word2
word1|word2
i tried several solutions but none works with all cases.The last one i tried is the 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 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);
This will give:
Array
(
[0] => word1&word2
[1] => word1&word2
[2] => word1&!word2
[3] => word1&!word2
[4] => word1|word2
[5] => word1|word2
)
这篇关于如何解析Postgresql查询的用户搜索字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!