Php 计算特定单词在文本中出现的次数?

Php 计算特定单词在文本中出现的次数?,php,jquery,Php,Jquery,我有个问题要问你们。假设我正在写一篇文章,我有10个关键词,它们应该在文本中提及。如果提到关键词,我需要计算这个词在文本中出现的次数。所有的金额都应该显示在文本区域的顶部或底部,例如在span或input中,这并不重要。但是怎么做呢 更新: 抱歉,我忘了在textarea中键入时要执行此操作,它需要在jquery中执行 function ck_jq() { var charsCount = CKEDITOR.instances['article'].getData().rep

我有个问题要问你们。假设我正在写一篇文章,我有10个关键词,它们应该在文本中提及。如果提到关键词,我需要计算这个词在文本中出现的次数。所有的金额都应该显示在文本区域的顶部或底部,例如在span或input中,这并不重要。但是怎么做呢

更新:

抱歉,我忘了在textarea中键入时要执行此操作,它需要在jquery中执行

function ck_jq()
{   



    var charsCount = CKEDITOR.instances['article'].getData().replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, '').replace(/^\s+|\s+$/g, '');
    var wordCount = CKEDITOR.instances['article'].getData().replace(/[^\w ]/g, "").split(/\s+/);
    var max = <?php echo $orderInfo->wordstarget; ?>;
    //var max = 5;
     if (wordCount >= max) {
          var over = max - wordCount.length;
            $("#wordstarget").css('color', 'red');
            $("#characterscount").css('color', 'red');
            $("#words").css('color', 'red');
            $("#wordsleft").css('color', 'red');
           // $("#wordscount").text(len.length + " characters and \n" + wordCount + " words in this text. Words target: " + max +". Words left: "+ char);       
            $("#wordstarget").text(max + " words target");       
            $("#characterscount").text(charsCount.length + " characters");       
            $("#words").text(wordCount.length + " words");       
            $("#wordsleft").text(over +" words left"); 

            //$("#wordscount").css('color', 'red');
            //$("#wordscount").text(len.length + " characters and \n" + wordCount + " words in this text. Words target: " + max +". Words left: "+ over);       
     } else {
          var char = max - wordCount.length;
            $("#wordstarget").css('color', 'green');
            $("#characterscount").css('color', 'green');
            $("#words").css('color', 'green');
            $("#wordsleft").css('color', 'green');
           // $("#wordscount").text(len.length + " characters and \n" + wordCount + " words in this text. Words target: " + max +". Words left: "+ char);       
            $("#wordstarget").text(max + " words target");                  
            $("#characterscount").text(charsCount.length + " characters");       
            $("#words").text(wordCount.length + " words");       
            $("#wordsleft").text(char +" words left");       
}

}
我用这个来计算单词和字符。CKEDITOR.instances['article'].getData使用它我可以获取所有CKEDITOR文本,然后搜索wor精确单词。

匹配部分单词foo匹配foobar

PHP:

JS:

仅匹配完整单词,不匹配部分单词

PHP:

JS:

更新:jsfunc

第三个参数strict默认为true,当设置为false时,它将不需要单词边界,允许foo匹配foobar

您可以使用此代码

echo substr_count($text, 'is');

实际上有一个内置函数来计算字符串中单词的出现次数,该函数名为,它返回一个单词数组,可以使用该数组进行计算,该数组提供了一个按单词索引的计数数组,然后可以使用关键字列表进行索引

编辑


您是否尝试过一些方法,或者只是需要其他人来解决您的问题?我在jquery中看到了很多代码示例,但这不是我需要的。我有从CKeditor阅读文本的脚本,我有计算所有单词和字符的脚本,但我找不到类似的东西,我想为特定的单词计数器。非常感谢。这东西很好用。谢谢@MariusGentvilas不用担心,很高兴能帮上忙-快乐编码!谢谢,你的回答很有帮助。我可以使用Xajax库用您的代码检查单词,谢谢!
"a foo bar of foos and such".match(/foo/g).length;//2
echo preg_match('#\bfoo\b#',"a foo bar of foos and such");//1
"a foo bar of foos and such".match(/\bfoo\b/g).length;//1
function word_count(str,word,strict)
{
    strict = typeof strict == "boolean" ? strict : true;
    var b = strict ? '\\b' : '';
    var rex = new RegExp(b+word+b,"g");
    return str.match(rex).length;
}
//where element.innerHTML = "a foo bar of foos and such"
word_count(element.innerHTML,'foo');//1
word_count(element.innerHTML,'foo',false);//2
echo substr_count($text, 'is');
$string = "It behooves us to offer the prospectus for our inclusive syllabus";
$keywords = array('syllabus', 'prospectus', 'inclusive');

$counts = array_intersect_key(
    array_count_values(
        str_word_count($string, 1)
    ),
    array_flip($keywords)
);
var_dump($counts);