PHP计数器返回所有Stopwords&;他们被发现了多少次?
我似乎找不到任何解决以下问题的方法,我想我会寻求帮助 我试图检索一个字符串中所有停止词(包括短语匹配词)的数组,以及找到每个停止词的次数。下面的代码是我能找到的最接近的代码,它将返回一个$counter值作为找到的stopwords的总数(尽管只有一个实例,而不是多个计数),并且显然没有列出这些单词 我尝试过使用preg_match_all和各种数组输出,但都导致了“抓头”错误 任何帮助都将不胜感激PHP计数器返回所有Stopwords&;他们被发现了多少次?,php,arrays,regex,counter,preg-match-all,Php,Arrays,Regex,Counter,Preg Match All,我似乎找不到任何解决以下问题的方法,我想我会寻求帮助 我试图检索一个字符串中所有停止词(包括短语匹配词)的数组,以及找到每个停止词的次数。下面的代码是我能找到的最接近的代码,它将返回一个$counter值作为找到的stopwords的总数(尽管只有一个实例,而不是多个计数),并且显然没有列出这些单词 我尝试过使用preg_match_all和各种数组输出,但都导致了“抓头”错误 任何帮助都将不胜感激 // test string $string = 'a string to see how ma
// 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”
等等
非常感谢
James只有在存在匹配项时才增加计数器,而不是增加匹配项的数量。使用
preg\u match\u all
并计算匹配结果的数量
$string='一个字符串要查看找到所有stopwords的次数,必须包含短语并返回所有stopwords的数组以及每个stopwords的找到次数'
// 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);
// 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));
演示:
您还可以使用|
对$stopwords
进行内爆如果其中永远不会有特殊字符,则不需要使用foreach
或用于每个匹配项的计数(这也使用内爆方法)
$string='一个字符串要查看找到所有stopwords的次数,必须包含短语并返回所有stopwords的数组以及每个stopwords的找到次数'
// 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);
// 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 = '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;
$string='string若要查看找到所有stopwords的次数,必须包含短语并返回所有stopwords的数组以及每个stopwords的找到次数';
$stopwords=array('all','times','words is found');
函数计数器\u字($string,$stopwords){
$output=array();
foreach($stopwords作为$stopword){
$pattern='/\b'.$stopword'.\b/i';
preg_match_all($pattern,$string,$matches);
$output[$stopword]=计数($matches[0]);
}
返回$output;
}
回声';打印(计数器字($string,$stopwords));出口
在这里测试Hi@chris85谢谢你的快速回复,这几乎就是我想要的。您能告诉我如何返回包含找到的每个停止字及其计数器值的关联数组吗?哦,错过了,好的,正在更新。。。如果这回答了问题,请记住接受它。天哪,你不知道我尝试这么做有多久了,哈哈。。。非常感谢你!我真的很感谢这些帮助和例子