PHPソリューションが望ましいですが、どんなアイデアでも素晴らしいでしょう。
テキストブロブを与える
'これは、赤いセーターと紫のゾウを見つけたいコンテンツのスーパー ストリングです。紫色のゾウは 2 回数えます。赤いセーターが 3 回出現するので、赤いセーターは 3 回カウントされます。
とフレーズリスト
「赤いセーター、紫のゾウ」
テキスト BLOB を検索して出現回数を返したい
したがって
赤いセーター = 3 と紫の象 = 2
http://www.php.net/manual/en/function.substr-count.php
$string = 'This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times';
$keys = 'red sweaters, purple elephants';
$pkeys = explode(', ', $keys);
foreach($pkeys as $key)
{
printf("%s occourrences: %d\n", $key, substr_count($string, $key));
}
テキスト内の文字列を検索するsubstr_countを使用できます。あなたの例では、テキストが「茶色のセーター」の場合、「赤いセーター」の場合は +1 とカウントされることに注意してください。
正規表現も使用できます。のようなものpreg_match("/$string/",$text);
。これは、文字列が見つかった時間を返します。
また、コンマで区切られた複数の文字列を検索する場合 (例のように)、最初に文字列を分割する必要があります。これには爆発を使用できます。$strings = explode(",",$search);
このようなものが動作するはずです:
<?php
$string = strtolower('This is a super string of some content whree I want to find red sweaters and purple elephants. The purple elephants will count twice. and the red sweaters will count 3 times since red sweaters occurs three times');
$allprases = 'red sweaters, purple elephants'
$phrasearray = explode(',',$allphrases);
foreach ($phrasearray as $k => $phrase) {
$phrase = strtolower(trim($phrase));
echo 'String '.$phrase.' found '.substr_count($string,$phrase).' times.<br />';
}
?>
substr_count は大文字と小文字が区別されることに注意してください (これが、上記のコードですべてを strtolower() している理由です)。これは簡単に削除できるので、上記のコードでも大文字と小文字が区別されます。