如何在mysql codeigniter php中使用select计算匹配的关键字


how to count the matched keywords using select in mysql codeigniter php?

如何在mysql codeigniter php中使用select计算匹配的关键字?这是我的表

交货。假设搜索关键字为yes,测试

messages table
id | title     | msg               |date
---+-----------+-------------------+-------------+--      
1  |  test1    |  yes              | 2016-06-01 // 2 match keywords
2  |  yes1     |  no               | 2016-06-02 // 1 match keywords   
3  |  test2    |  no               | 2016-06-03 // 1 match keywords
4  |  yes2     |  yes yes yes      | 2016-06-04 // 4 match keywords
5  |  yesyes3  |  yes yes yes yes  | 2016-06-05 // 6 match keywords

现在需要用count_match列

显示它
id | title     | msg               |date        |count_match    
---+-----------+-------------------+------------+-------------------    
1  |  test1    |  yes              | 2016-06-01 | 2
2  |  yes1     |  no               | 2016-06-02 | 1  
3  |  test2    |  no               | 2016-06-03 | 1
4  |  yes2     |  yes yes yes      | 2016-06-04 | 4
5  |  yesyes3  |  yes yes yes yes  | 2016-06-05 | 6

对于数组,它应该像这样显示

array (
    [0] => array (
        [id] => 5
        [title] => yesyes3
        [msg] => yes yes yes yes
        [date] => 2016-06-05
        [match] => 6
    )
    [1] => array (
        [id] => 4
        [title] => yes2
        [msg] => yes yes yes
        [date] => 2016-06-04
        [match] => 4
    )
    [2] => array (
        [id] => 1
        [title] => test1
        [msg] => yes
        [date] => 2016-06-01
        [match] => 2
    )
    [3] => array (
        [id] => 3
        [title] => test2
        [msg] => no
        [date] => 2016-06-03
        [match] => 1
    )
    [4] => array (
        [id] => 2
        [title] => yes1
        [msg] => no
        [date] => 2016-06-02
        [match] => 1
    )
)

目前我在取回这个的代码是这个

        $match = array('test','yes');
        $orderbyString1 = "";
        $orderbyString = "";
        $likestr = "";
        foreach($match AS $value)
        {
            $orderbyString .= "IF(m.title LIKE '%".$value."%' OR m.msg LIKE '%".$value."%',1,0)+";
            $likestr .= "m.title LIKE '%".$value."%' OR m.msg LIKE '%".$value."%' OR ";
        }
        $orderbyString = substr($orderbyString, 0, -1);
        $likestr = substr($likestr, 0, -4);
        $this->db->select('m.*, ('.$orderbyString.') as count_match');
        $this->db->from('messages m');
        $this->db->where($likestr." ORDER BY ".$orderbyString." DESC");
        $query = $this->db->get();
        $results = $query->result_array();
        print_r($results);exit;

是否有任何方法为选择显示正确的count_match使用php编码器代码?因为目前它在count_match下显示1和2。

谢谢!

在php端有许多选项来计算数组中的关键字。如果你需要额外的功能,如无大小写匹配或单词边界,如何使用regex。

关于preg_match_all的一个想法

返回完整模式匹配的数目(可能为零),如果发生错误则返回FALSE。

$pattern = '~(?:yes|test)~i';
foreach($arr AS $k => $v)
  $arr[$k]['match'] = preg_match_all($pattern, $v['title']." ".$v['msg']);

模式只是使用非捕获组替换这两个关键字。在结束模式分隔符~之后,使用i标志进行无大小写匹配。Regex101是测试模式的好地方。

下面是eval.in

的示例

如果输入是通用的,使用preg_quote将某些字符从它的特殊regex含义中转义。