Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP str_word_count() Function

PHP str_word_count() Function

PHP String Reference Manual PHP String Reference Manual

Example

Count the words in the string "Hello World!":

Run Example »


Definition and Usage

The str_word_count() function counts the words in a string.


Syntax

str_word_count(

string,return,char

)

Parameters Description
string Required. Specifies the string to check.
return Optional. Specifies the return value of the str_word_count() function.

Possible values:

  • 0 - Default. Returns the number of words found.
  • 1 - Returns an array containing the words in the string.
  • 2 - Returns an array where the keys are the positions of the words in the string and the values are the actual words.
  • | | *char* | Optional. Specifies special characters to be recognized as part of a word. |

    Technical Details

    Return Value: Returns a number or an array, depending on the selected return parameter was added.
    PHP Version: 4.3.0+
    Changelog: In PHP 5.1, the char parameter was added.

    More examples

    Example 1

    Return an array containing the words in the string:

    Run Example »

    Example 2

    Return an array where the keys are the positions of the words in the string and the values are the actual words:

    Run Example »

    Example 3

    With and without the char parameter:

    Run Example »


    PHP String Reference Manual PHP String Reference Manual