PHP MySQL中的多个关键字搜索

时间:2018-07-08 09:51:20

标签: php mysql database search keyword-search

这是我从表单输入的post方法。该表格包括搜索技术。 What are the difference between AC motor and DC motor?

我希望输出为 What|difference|AC|motor|DC|motor

但是我得到了输出 What|||difference||AC|motor||DC|motor

我在做什么错了?


这是我的编码方法

<?php
include 'dbh.inc.php';

if(isset($_POST['submit']) && !empty($_POST['search'])){

    $value = trim(mysqli_real_escape_string($conn, $_POST['search']));
    $noSpace = preg_replace('/\s+/', ' ', $value);
    $noCommon = removeCommonWords($noSpace);

    $replace = str_replace(' ', '|', $noCommon);

    echo $replace;

}



function removeCommonWords($input){

    // EEEEEEK Stop words
    $commonWords = array('a','able','about','above','abroad',..........);

    return preg_replace('/\b('.implode('|',$commonWords).')\b/','',$input);
}


?>

1 个答案:

答案 0 :(得分:1)

您可以匹配并跳过 常见单词,仅匹配并保留其他单词块:

\b(?:are|the|between|and)\b(*SKIP)(*F)|\w+

请参见regex demo

详细信息

  • \b(?:are|the|between|and)\b-整个单词are,等等。
  • (*SKIP)(*F)-PCRE动词,该动词会丢弃匹配项,并从不成功匹配项的末尾继续查找下一个
  • |-或
  • \w+-仅匹配并保留1个或多个单词字符。

这里是PHP snippet

$commonWords = ['are','the','between','and'];
$value = 'What are the difference between AC motor and DC motor?';
$rx = '~\b(?:' . implode('|', $commonWords) . ')\b(*SKIP)(*F)|\w+~u';
if (preg_match_all($rx, $value, $matches)) {
    echo implode('|', $matches[0]); // => What|difference|AC|motor|DC|motor
}