标签: preg-grep

如何在PHP中迭代多个具有多个数组值的字符串?

*来自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);
Run Code Online (Sandbox Code Playgroud)

如何将它打印出"七十五码外",因为它实际上最接近文本?我正在考虑将字数除以得到一个百分比,但现在认为现在可能有效了..

php arrays string loops preg-grep

5
推荐指数
1
解决办法
131
查看次数

标签 统计

arrays ×1

loops ×1

php ×1

preg-grep ×1

string ×1