Home >>PHP String Functions >PHP str_word_count() Function
PHP str_word_count() function is used to get the information about the words used in a given input string. It returns the information like total number of words present in the string, positions of the words in the string.
Syntax:
str_word_count($string , $return, $char);
Parameter | Description |
---|---|
string | This is a required parameter. This parameter contains the string to check. |
return | This is an optional parameter. This parameter defines the return value of the function. Possible values:
|
char | This is an optional parameter. This parameter defines special character to be considered as word. |
Here is an example of str_word_count() function in PHP:
<html> <body> <?php $str = "CodeIgniter is an Open Source PHP Framework. It has a very rich set of functionality, which will increase the speed of website development work."; echo str_word_count($str); ?> </body> </html>
Here is an another example of str_word_count() function in PHP using different $return values:
<html> <body> <pre> <?php $str = "CodeIgniter is an Open Source PHP Framework."; print_r (str_word_count($str,1)); // returns an array with all the words echo "<br>"; print_r (str_word_count($str,2)); // returns an array with all the words and their positions ?> </pre> </body> </html>
Here is an another example of str_word_count() function in PHP using the $char values:
<html> <body> <pre> <?php $str = "CodeIgniter & Laravel is an Open Source PHP Framework."; print_r(str_word_count($str,1)); print "<br>"; print_r(str_word_count($str,1,"&")); ?> </pre> </body> </html>