2016-03-24 11 views
1

文をより小さな文に分割してデータベース検索を実行する方法がありますか?私のクライアントは、この例のように、データベースの検索を実行したい:PHPを使用して文を短い文に分割する

最初の要求を:請求者には、コーヒーショップ 、第2の要求に事故にあった:請求者は、コーヒーに(in case document states it was a bar etc.) 第三の要求を、事故があった:請求者は、事故がありました ... 最後のリクエスト:申請者

私は、senteceを単語で分割することに関する多くのトピックを見つけましたが、単語の塊については何も見つかりませんでした。助言がありますか?

+2

この特定の猫を肌には多くの方法があります。 –

+0

タスク内のさまざまな要求に対して「回転」メカニズムをどのように定義する必要がありますか?つまり、どのように多くの単語に各リクエストが含まれているのか、プログラムはどのように認識していますか? – RomanPerekhrest

+0

@RomanPerekhrestの場合、それぞれの新しいリクエストは、前のリクエストから1ワードを差し引いたものに等しくなければなりません。 –

答えて

1

シンプルなソリューション機能:

$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>"; 
} 

出力:

01文字列関数を使用して
+0

あなたは本当のMVPです! –

+0

それを聞いてうれしいです!どうもありがとうございました ) – RomanPerekhrest

1

explode(" ", $str)を使用できます。次に、単語ごとに文の単語を再構成することができます(特定の反復以外の単語を除く)。あなたは句読点に基づいて分割正規表現を書くことができ

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

私はあなたのアプローチを逆に使用すると、それはどういうわけか正しいです。 –

+0

@IvanVenediktov気軽に使ってみてください。あなたが役に立つと分かったら、私の答えを受け入れることもできます。 – Laurel

0

:この(ループがオフ、私は前にPHPを書いたことがないこともある)のような

何か。期間、疑問符、感嘆符や行末に分割し、あなたが始める必要があります。この例で、:

$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"; 
    } 
} 

結果:explodeimplodearray_slice

Sentence: First request: Claimant lost $500 in the coffee shop. 
Sentence: Second request: (unknown) Claimant had an accident? 
Sentence: Third request: Claimaint went to the hospital! 
Sentence: End of report 
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>"; 
}