使用PHP将句子分成更短的句子

时间:2016-03-24 21:45:35

标签: php string

我是否可以通过某种方式将句子拆分为较小的句子来执行数据库搜索。我的客户想要执行数据库搜索,如下例所示:

第一要求:索赔人在咖啡店发生意外 第二个要求:索赔人在咖啡中发生意外(in case document states it was a bar etc.) 第三个要求:索赔人在事故中发生了意外 ... 上次要求:索赔人

我发现很多关于按字词分割sentece的话题,但没有关于词块的话题。有什么建议吗?

4 个答案:

答案 0 :(得分:1)

您可以使用explode(" ", $str)。然后,您可以逐字重建句子(除了特定迭代之外的单词)。

这样的事情(循环可能已关闭,我以前从未编写过PHP):

for ($x = count($sentence); $x >0; $x--) {
    for($y = 0; $y < $x; $y++) {
      echo $cars[$y];
       echo "<br>";
   }
   echo "<br>"
} 

答案 1 :(得分:1)

使用explodeimplodearray_slice函数的简单解决方案:

$str = "Claimant had an accident in the coffee shop";
$words = explode(" ", $str);
$count = count($words);

echo implode(" ", array_slice($words, 0, $count)) . "<br>"; // first request
while (--$count) {
    echo implode(" ", array_slice($words, 0, $count)) . "<br>";
}

输出:

Claimant had an accident in the coffee shop
Claimant had an accident in the coffee
Claimant had an accident in the
Claimant had an accident in
Claimant had an accident
Claimant had an
Claimant had
Claimant

答案 2 :(得分:1)

使用字符串函数的另一个选项:

$string = 'Claimant had an accident in the coffee shop';

echo "$string<br>";  // use the entire string first as first iteration of
                     // the loop will chop off the last word

while ($string = substr($string, 0, strrpos($string, ' '))) {
    echo "$string<br>";
}

答案 3 :(得分:0)

你可以编写一个基于标点符号分割的正则表达式。这个例子分为句点,问号,感叹号和行尾,应该让你开始:

// +build !windows

结果:

$data = 'First request: Claimant lost $500 in the coffee shop.  Second request: (unknown) Claimant had an accident?  Third request: Claimaint went to the hospital!  End of report';

preg_match_all("/\s*(.*?(?:[\.\?\!]|$))/", $data, $matches);
foreach ($matches[1] as $sentence) {
    if (preg_match("/\S/", $sentence) ) {
        print "Sentence: $sentence\n";
    }
}