توابع str_word_count() در PHP

مثال

حساب تعداد کلمات در رشته "I love Shanghai!":

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

Run Example

Definition and Usage

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

Syntax

str_word_count(string,return,char)
Parameter Description
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 the words in the 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 the special characters considered as words.

Technical Details

Return Value: Returns a number or an array depending on the choice return Parameter.
PHP Version: 4.3.0+
Update Log: In PHP 5.1, a new feature was added: char Parameter.

More Examples

Example 1

Returns an array containing the words in the string:

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

Run Example

Example 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:

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

Run Example

Example 3

Setting and not setting char parameter:

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

Run Example