1

str_word_count()からコンテンツの単語数を計算するために使用していCKEditorます。CKEditior から取得したコンテンツは HTML コンテンツであり、単語数を計算する必要があります。MS ワードでは、単語数は 328 になります。一方、html タグではstr_word_count()、362 ワードを使用した後にコンテンツから取得します。PHP文字列変数からHTMLタグを削除する方法はありますか? を使用しようとしたところstrip_tags()、336 になりました。PHP で正確な単語数を取得する方法はありますか? 前もって感謝します。

たとえば、このようなユーザーが入力したこのエッセイ。

混合学校またはユニセックス学校

混合学校が生徒に与える影響について考えたことはありますか? アメリカのほとんどの学校は男女混合です。つまり、女の子と男の子が同じ教室で一緒に勉強しています。一部の親は、混合学校またはユニセックス学校のいずれかの学校での子供の影響について疑問に思います. これらの影響は教育だけに関するものではなく、彼らの性格、異性との行動、そして最終的には教育に関するものです。私の意見では、10 代の生徒のためのユニセックスの学校は男女混合の学校よりもはるかに優れていると思います。この結論は多くの理由に基づいています。

MSワードでは、ワード数は次のとおりです。107

PHPで

 

混合学校またはユニセックス学校

 

混合学校が生徒に与える影響について考えたことはありますか? アメリカのほとんどの学校は男女混合です。つまり、女の子と男の子が同じ教室で一緒に勉強しています。一部の親は、混合学校またはユニセックス学校のいずれかの学校での子供の影響について疑問に思います. これらの影響は教育だけに関するものではなく、彼らの性格、異性との行動、そして最終的には教育に関するものです。私の意見では、10 代の生徒のためのユニセックスの学校は男女混合の学校よりもはるかに優れていると思います。この結論は多くの理由に基づいています。

そして結果:114

1 段落のエッセイに 7 語余分に計算しています。

編集

使用後

    $text = strip_tags($this->orginal_content);
    $text = str_replace(' ',"",$text);
    $this->orginal_content_count = str_word_count($text);

結果:112

3つのスペースを見つけました

        Mixed School or Unisex School       Have you ever think about the impact of mixed schools for students? Most of the schools in the U.S are mixed gender, which mean girls and boys are studying with each other in the same classroom. Some parents wonder about the influences of their child’s in the school either in mixed school or in unisex ones. These influences are not about the education only, the influences about their personality, behavior with the opposite sex and finally their education. In my opinion, I think the unisex schools for teenager’s students are much better than mixed schools, and this conclusion based in many reasons. 
4

1 に答える 1

2

Okay.

You already know about strip_tags(). That's a good start.

You're replacing   with a space, but that only deals with that single specific entity. You would be better off using PHP's html_entity_decode() function which will get rid of all of the entity codes from your string.

If extra spacing is causing you problems, you could try doing str_replace() or preg_replace() to get rid of them. eg:

$output = preg_replace('/\s\s+/',' ',$input);

This will convert all multiple-whitespace instances into a single space character.

Now your word count should work a little better.

Hope that helps.

于 2012-05-20T06:41:11.827 に答える