0

皆さんに質問があります。私が記事を書いているとしましょう。10 個のキーワードがあり、テキストで言及する必要があります。キーワードが言及されている場合、この単語がテキストに含まれていた回数を数える必要があります。そして、すべての金額は、テキストエリアの上部または下部に表示する必要があります。たとえば、スパンや入力など、問題ではありません。しかし、どのように?

アップデート:

申し訳ありませんが、テキストエリアに入力するときにやりたいことを忘れていました.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

3 番目のパラメーター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を使用してカウントできる単語の配列を返します。これにより、単語でインデックス付けされたカウントの配列が得られます。キーワードのリスト。

編集

$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 に答える