我正在使用preg_split查找一些结果,除了(奇怪的是)第一个单词外,它工作得很好。
例如,如果有一个名为“电话蓝”的项目,而我搜索“电话随机”,我什么也得不到,但是如果我搜索“随机电话”(或“某某物ph",”某某电话随机“等),它就会发现”手机蓝色“。
我到处寻找不同的表达方式,并尝试过它们(例如@\W+@、/\s、+/等)。但什么都帮不上忙。我只能假设第一次尝试找到这个项目(用完整的字符串)会把事情搞砸,但不知道是怎么回事。对这件事很陌生!谢谢你的帮助
编辑:忘了只搜索“电话”也不起作用。
function find_by_location_or_name($where) {
// first try complete
$this->db->where('items_location', $where);
$this->db->or_like('items_name', $where);
$query = $this->db->from('items_table')->get();
// if nothing, break it up, try again
if( !$query->num_rows) {
$where_like = preg_split('/ /', $where);
$tryagain = false;
foreach($where_like as $like) {
if(! $tryagain) {
$this->db->like('items_location', $like);
} else {
$this->db->or_like('items_name', $like);
}
$tryagain = true;
}
// Do the query
$query = $this->db->from('items_table')->get();
}更新:感谢大家的帮助!到头来,这起了作用:
// first try complete
$this->db->where('items_location', $where);
$this->db->or_where('items_name', $where);
$query = $this->db->from('items_table')->get();
// if nothing, break it up, try again
if( !$query->num_rows) {
$where_like = preg_split('/ /', $where);
$tryagain = false;
foreach($where_like as $like) {
if(! $tryagain) {
$this->db->like('items_location', $like);
$this->db->or_like('items_name', $like);
} else {
$this->db->like('items_name', $like);
}
$tryagain = true;
}发布于 2013-11-07 12:02:19
我看了你的密码。而不是preg_split,您应该执行全文索引,并将整个字符串交给MySQL。让MySQL为你找到它。这正是你所需要的。
请看:http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
这个链接通过示例更清楚:http://www.bakale.com/mysql/myfultext.htm
https://stackoverflow.com/questions/19835306
复制相似问题