Current Location: Home> Function Categories> str_word_count

str_word_count

Return information about the words used in the string
Name:str_word_count
Category:String
Programming Language:php
One-line Description:Calculate the number of words in the string.

Definition and usage

str_word_count() function calculates the number of words in a string.

Example

Example 1

Calculate the number of words in the string "I love Shanghai!":

 <?php
echo str_word_count ( "I love Shanghai!" ) ;
?>

Try it yourself

Example 2

Returns an array containing words in a string:

 <?php
print_r ( str_word_count ( "I love Shanghai!" , 1 ) ) ;
?>

Try it yourself

Example 3

Returns an array where the key name is the position of the word in the string and the key value is the actual word:

 <?php
print_r ( str_word_count ( "I love Shanghai!" , 2 ) ) ;
?>

Try it yourself

Example 4

Set and not set char parameters:

 <?php
print_r ( str_word_count ( "I love Shanghai & good morning!" , 1 ) ) ;
print_r ( str_word_count ( "I love Shanghai & good morning!" , 1 , "&" ) ) ;
?>

Try it yourself

grammar

 str_word_count ( string , return , char )
parameter describe
string Required. Specifies the string to be checked.
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 words in a string.
  • 2 - Returns an array where the key name is the position of the word in the string and the key value is the actual word.
char Optional. Specifies special characters that are considered words.
Similar Functions
Popular Articles