[英]How to get Sentence From Long String which contains specific Word using PHP
我知道如何檢查字符串是否包含特定單詞
if (str_contains('How are you', 'are')) {
echo 'true';
}
但我正在尋找給我的用戶提供如下搜索結果
所以如果我的字符串如下所示
段落的展開從控制思想的形成開始。 這個想法指導了段落的發展。 通常,段落的控制思想會以主題句的形式出現。 在某些情況下,您可能需要多個句子來表達段落的控制思想。
然后如果我的用戶搜索像idea directs
這樣的詞,那么我想顯示像This idea directs the paragraph's development
結果
我不知道哪個 PHP function 可以幫助我實現我的目標。 讓我知道這里是否有人可以幫助我實現我的目標。 謝謝!
沒有簡單的 function 可以直接實現。 你將不得不編寫一個算法來完成它。 下面類似的東西。
$text = "Paragraph development begins with the formulation of the controlling idea. This idea directs the paragraph’s development. Often, the controlling idea of a paragraph will appear in the form of a topic sentence. In some cases, you may need more than one sentence to express a paragraph’s controlling idea.";
$search = "idea directs";
$sentenses = explode(".", $text);
for($i = 0; i < sizeof($sentenses); $i++){
if(strpos($sentenses[$i], $search))
echo $sentenses[$i];
}
將文本轉換為小寫將幫助您搜索不區分大小寫,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.