PHP str_word_count() Function
Halimbawa
Tinuturing ang bilang ng mga salita sa string "I love Shanghai!":
<?php echo str_word_count("I love Shanghai!"); ?>
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:
|
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)); ?>
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)); ?>
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,"&")); ?>