0

我有问题要问你们。假设我正在写一篇文章,我有 10 个关键字,它们应该在正文中提及。如果提到关键字,我需要计算这个词在文本中出现的次数。并且所有数量都应该显示在 textarea 的顶部或底部,例如跨度或输入,这无关紧要。但是怎么做?

更新:

对不起,我忘了提到我想在输入 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 文本,然后搜索确切的单词。

4

3 回答 3

6

匹配部分单词(foo 匹配 foobar)

PHP:

echo substr_count("a foo bar of foos and such","foo");//2

JS:

"a foo bar of foos and such".match(/foo/g).length;//2

只匹配完整的单词,没有部分匹配

PHP:

echo preg_match('#\bfoo\b#',"a foo bar of foos and such");//1

JS:

"a foo bar of foos and such".match(/\bfoo\b/g).length;//1

PHP的文档substr_count()

更新:JS函数

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

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

于 2013-08-19T13:46:52.930 回答
3

你可以使用这个代码

echo substr_count($text, 'is');

http://www.php.net/manual/en/function.substr-count.php

于 2013-08-19T13:47:06.200 回答
2

实际上有一个内置函数来计算字符串中单词的出现次数,称为str_word_count(),它返回一个可以使用array_count_values计算的单词数组,它给出了一个按单词索引的计数数组,然后你可以使用你的array_intersect_key()关键字列表。

编辑

$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);
于 2013-08-19T13:48:54.090 回答