Forum und email

str_word_count

(PHP 4 >= 4.3.0, PHP 5)

str_word_count — 문자열에서 사용한 단어에 대한 정보를 반환합니다.

설명

mixed str_word_count ( string $string [, int $format ] )

string 안의 단어 수를 셉니다. 선택적인 format 을 지정하지 않으면, 반환값은 단어 수를 나타내는 정수입니다. format 를 지정하면, 반환값은 배열로써, 내용은 format 에 따라 결정됩니다. 아래 목록은 format 으로 사용할 수 있는 값과 그에 따른 출력입니다.

  • 1 - string 에서 발견한 모든 단어를 포함하는 배열을 반환합니다.
  • 2 - string 안에서 단어 위치를 키로, 그 단어를 값으로 가지는 연관 배열을 반환합니다.

이 함수의 작동에서, '단어'는 알파벳 문자를 포함하는 로케일 의존 문자열로, "'"'와 "-" 문자로 시작하지 않는 문자들을 포함합니다.

Example#1 str_word_count() 사용 예제

<?php

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

$a   str_word_count($str1);
$b   str_word_count($str2);
$c   str_word_count($str);

print_r($a);
print_r($b);
echo 
$c;
?>

출력은:


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

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

6

참고: explode(), preg_split(), split(), count_chars(), substr_count().