PHP计数器返回所有停用词&他们被发现了多少次?

时间:2017-01-04 14:13:06

标签: php arrays regex counter preg-match-all

我似乎无法找到解决以下问题的任何事情,并认为我会寻求帮助。

我正在尝试检索字符串中所有停用词(包括词组匹配词)的数组,以及每次找到的次数。以下代码是我最接近的代码,它将为找到的停用词总数返回$ counter值(仅限单个实例,而不是多个计数),显然不会列出单词。

我尝试过使用preg_match_all和各种数组输出,这些都导致了#34;头部刮擦"错误。

任何帮助都将不胜感激。

// test string
$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';

// test stopwords
$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {

$counter = 0;   

foreach ($stopwords as $stopword) {

    $pattern = '/\b' . $stopword . '\b/i';              
    if (preg_match($pattern, $string)) {
        $counter++;
    }
}

return $counter;
}

// test - output counter only
echo counter_words($string, $stopwords);

通过一些修改,我希望能够返回一个数组(可能是一个相关的数组),我可以在其中回显类似于:

找到的单词/短语:"找到单词",找到实例" 1"

找到的单词/短语:"次",找到实例" 1"

等...

非常感谢

詹姆斯

2 个答案:

答案 0 :(得分:0)

如果匹配,您只是增加计数器,而不是匹配数量。使用preg_match_all并计算匹配结果的数量。

$ string ='一个字符串,用于查看找到所有停用词的次数,必须包含短语并返回所有停用词的数组以及每次停用词的次数';

// test stopwords
$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {

$counter = 0;   

foreach ($stopwords as $stopword) {
    $pattern = '/\b' . $stopword . '\b/i';              
        if (preg_match_all($pattern, $string, $matches)) {
             $counter += count($matches[0]);
        }
    }
    return $counter;
}

// test - output counter only
echo counter_words($string, $stopwords);

演示:https://eval.in/709349

如果implode $stopwords | foreach,如果那里永远不会有特殊字符,那么你就不需要implode

...

或每个匹配术语的计数(这也使用// test stopwords $stopwords = array('all','times','words are found'); function counter_words($string, $stopwords) { $pattern = '/\b' . implode('|', $stopwords) . '\b/i'; preg_match_all($pattern, $string, $matches); return !empty($matches) ? array_count_values($matches[0]) : 'No matches found'; } // test - output counter only print_r(counter_words($string, $stopwords)); 方法)。

$ string ='一个字符串,用于查看找到所有停用词的次数,必须包含短语并返回所有停用词的数组以及每次停用词的次数';

apply plugin: 'com.google.gms.google-services'

演示:https://eval.in/709369

答案 1 :(得分:0)

检查一下。它将返回单个数组中所有单词的计数器:

$string = 'a string to see how many times all stopwords words are found, must include phrases and return an array of all stopwords and how many times each was found';


$stopwords = array('all','times','words are found');

function counter_words($string, $stopwords) {
    $output = array();

    foreach ($stopwords as $stopword) {
        $pattern = '/\b' . $stopword . '\b/i';
        preg_match_all($pattern, $string, $matches);
        $output[$stopword] = count($matches[0]);
    }
    return $output;
}

echo '<pre>';print_r(counter_words($string, $stopwords));exit;

在这里测试https://eval.in/709375