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
  • Convert logical-Hebrew to visual-Hebrew and convert newlines hebrevc

    hebrevc

    Convertlogical-Hebre
  • Find the first occurrence of substrings in strings strpos

    strpos

    Findthefirstoccurren
  • Find the first occurrence of a string strstr

    strstr

    Findthefirstoccurren
  • Set the first character of the string to uppercase ucfirst

    ucfirst

    Setthefirstcharacter
  • Calculate the similarity between two strings similar_text

    similar_text

    Calculatethesimilari
  • Calculate the sha1 hash value of a string sha1

    sha1

    Calculatethesha1hash
  • Binary safe case-insensitive string comparison strcasecmp

    strcasecmp

    Binarysafecase-insen
  • Calculate the soundex key of a string soundex

    soundex

    Calculatethesoundexk