Computer >> Computer tutorials >  >> Programming >> PHP

str_word_count() function in PHP


The str_word_count() function returns information about words used in a string.

Syntax

str_word_count(str, return, char)

Parameters

  • str − The string to check

  • return − The return value of the str_word_count() function.

  • The following are the possible values −

    • − It returns the number of words found

    • − It returns an array with the words from the string

    • − It returns an array where the key is the position of the word in the string, and value is the actual word.

  • char − The special characters to be considered as word.

Return

The str_word_count() function returns an array or number. It depends on the return parameter.

Example

The following is an example −

<?php
print_r(str_word_count("Demo text!",1));
?>

Output

Array (
   [0] => Demo
   [1] => text
)