str_word_countとは? わかりやすく解説

Weblio 辞書 > コンピュータ > PHP関数リファレンス > str_word_countの意味・解説 

str_word_count

(PHP 4 >= 4.3.0, PHP 5)
str_word_count — 文字列に使用されている単語についての情報を返す

説明

mixed str_word_count ( string string [, int format [, string charlist]] )
string の単語数を数えます。 オプションの format が指定されていない場合、 見つかった単語の数を整数値で返します。 format が指定されている場合は結果が配列で返され、 配列の内容は format に依存します。 format に設定できる値と対応する出力については 以下で示します。
この関数を使用するうえで、'単語' は以下のように定義されます。すなわち、 「ロケールに依存しないアルファベットからなる文字列で、その先頭以外の部分に "'" および "-" が含まれていてもよい」。

パラメータ

string
文字列。
format
この関数の戻り値を設定します。現在サポートされている値は 以下のとおりです。
  • 0 - 見つかった単語の数を返します。
  • 1 - string の中に見つかった単語を含む 配列を返します。
  • 2 - 連想配列を返します。string の中での 単語の開始位置がキー、単語自体を対応する値となります。

charlist
'単語' とみなされる文字に追加する文字のリスト。

返り値

選択した format に応じて、配列あるいは整数を返します。

変更履歴

バージョン説明
5.1.0 charlist パラメータが追加されました。

例 2304. str_word_count() の例
<?php

$str = "Hello fri3nd, you're
       looking          good today!";

print_r(str_word_count($str, 1));
print_r(str_word_count($str, 2));
print_r(str_word_count($str, 1, 'àáãç3'));

echo str_word_count($str);

?>
上の例の出力は以下となります。

Array
(
    [0] => Hello
    [1] => fri
    [2] => nd
    [3] => you're
    [4] => looking
    [5] => good
    [6] => today
)

Array
(
    [0] => Hello
    [6] => fri
    [10] => nd
    [14] => you're
    [29] => looking
    [46] => good
    [51] => today
)

Array
(
    [0] => Hello
    [1] => fri3nd
    [2] => you're
    [3] => looking
    [4] => good
    [5] => today
)

7

    


参考

explode()
preg_split()
split()
count_chars()
substr_count()




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「str_word_count」の関連用語

str_word_countのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



str_word_countのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
PHP Documentation GroupPHP Documentation Group
Copyright © 1997 - 2024 by the PHP Documentation Group.

©2024 GRAS Group, Inc.RSS