2013-08-19 193 views
0

我有問題給你們。比方說,我正在寫一篇文章,我有10個關鍵字,他們應該在文中提到。如果提到關鍵字,我需要計算這個單詞在文本中的次數。所有的數量應該顯示在textarea的頂部或底部,例如跨度或輸入,這並不重要。但如何?計算在文本中出現特定單詞的次數?

UPDATE:

對不起,我忘了提,我希望當我在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文本,然後搜索確切的單詞。

+1

你有沒有嘗試過的東西或者只是需要別人解決問題了嗎? –

+0

我在jQuery中看到了很多代碼示例,但那不是我所需要的。我有從CKeditor閱讀文本腳本,我有腳本來計算所有的單詞和字符,但我無法找到類似的東西我想要的特定單詞計數器。 –

回答

5

匹配部分單詞(富匹配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 

Doc for PHP's substr_count()

更新:JS FUNC

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

+0

非常感謝。這件事情效果很好。謝謝! –

+0

@MariusGentvilas不用擔心,很高興有幫助 - 快樂的編碼! – SmokeyPHP

1

實際上,有一個內置的函數來計算的話出現在一個名爲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); 
+0

謝謝,你的回答非常有幫助。我可以使用Xajax庫檢查您的代碼,謝謝! –

相關問題