PHP str_word_count() Function


The str_word_count() string function is used to counts the number of words in a string.

Syntax
str_word_count(string,return,char)
Parameters
  • string - (required) Specify the string to check.
  • return - (optional) Specify return value of the function.
    value 0 - it is default value and return the number of words found.
    value 1 - it returns array with the words.
    value 2 - it returns value is the actual word.
  • char - (optional) Specify the characters.
Example
<?php
    echo str_word_count("How are you!");
?>

Output

3
Example
<?php
    print_r(str_word_count("Hello sir, Have a nice day!",1));
    print "<br>";
    print_r(str_word_count("Hello sir, Have a nice day!",1,","));
?>

Output

Array ( [0] => Hello [1] => sir [2] => Have [3] => a [4] => nice [5] => day )
Array ( [0] => Hello [1] => sir, [2] => Have [3] => a [4] => nice [5] => day )

Prev Next