【问题标题】:5 consecutive words in a sentance to check with mysql database用mysql数据库检查一个句子中的5个连续单词
【发布时间】:2017-07-17 07:17:16
【问题描述】:

请帮帮我,我想用 PHP、MySql
假设我们有一个句子

“一个语法独立的语法单元,有一个可以表达的主语”

我要检索与上句中任意连续五个单词匹配的所有数据,即
“一个语法单位” OR
“语法上的语法单元” OR
“语法独立的单元” OR 等

我们可以使用正则表达式匹配或任何简单的解决方案吗?
期待你
谢谢

【问题讨论】:

标签: php mysql regex


【解决方案1】:

如果你想用 PHP 来做,你可以试试这样:

  $sentence = "A grammatical unit that is syntactically independent and has a subject that is expressed";

$arr = explode(" ", $sentence);

var_dump($arr);
for ($i = 0; $i < (count($arr) - 5); $i++) {

      $newSentence = $arr[$i] + " " + $arr[$i + 1] + " " + $arr[$i + 2] + " " + $arr[$i + 3] + " " + $arr[$i + 4];
  //do database inset here

}

【讨论】:

    【解决方案2】:

    您可以将条件放在数据库级别,在 mysql 中您可以这样做:

     select * from table_name where position('A grammatical unit that is' in field_name) > 0 or position('grammatical unit that is syntactically' in stradacstm) > 0 or position('unit that is syntactically independent' in stradacstm) > 0;
    

    【讨论】:

    • 感谢您提供此代码 sn-p,它可能会提供一些即时帮助。一个正确的解释would greatly improve 其教育价值通过展示为什么这是一个很好的解决问题的方法,并将使它对未来有类似但不相同的问题的读者更有用。请edit您的答案添加解释,并说明适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 2014-12-31
    • 2022-09-23
    • 2016-01-16
    • 2019-01-06
    • 2022-06-19
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多