【发布时间】:2016-11-27 00:22:07
【问题描述】:
* revo 回答中的更新问题
这是一个工作脚本,其中包含一组更好的示例字符串来显示我的意图-
$strings[] = 'seventy five yards out';
$strings[] = 'sixty yards out';
$strings[] = 'one hundred fifty yards out';
$inputString = 'seventy two yards out';
$inputWords = str_word_count($inputString, 1);
$foundWords = [];
foreach ($strings as $key => $string) {
$stringWords = str_word_count($string, 1);
$wordsCount = array_count_values($stringWords);
$commonWords = array_intersect($inputWords, array_keys($wordsCount));
if (count($commonWords) > 0) {
foreach ($commonWords as $commonWord) {
$foundWords[$key][$commonWord] = $wordsCount[$commonWord];
}
}
}
print_r($foundWords);
我如何让它打印出“七十五码”,因为它实际上是最接近文本的?我正在考虑将字数除以得到一个百分比,但现在认为现在可能可行..
【问题讨论】:
标签: php arrays string loops preg-grep