Monday 29 September 2014

str_word_count in PHP

PHP str_word_count() function is utilized to count the number of words in a string.

Syntax:

str_word_count(string,return,char)
string : Required. Defines the input string.

return : Optional. Defines the return value of the PHP str_word_count()

char : Optional. Defines special characters to be considered as words

Example:)

<?php
echo str_word_count("Good Morning.");
echo "<br /><stromg> Example with an return parameter : </stromg><br />";
print_r(str_word_count("Good Morning Sam & John.",1));
echo "<br /><stromg> Example with an char parameter : </stromg><br />";
print_r(str_word_count("Good Morning Sam & John.",1,"&"));
?>
Output  will be:
2
Example with an return parameter :
 Array ( 
        [0] => Good 
        [1] => Morning 
        [2] => Sam 
        [3] => John 
        )
Example with an char parameter :
 Array (
         [0] => Good 
         [1] => Morning 
         [2] => Sam 
         [3] => & 
         [4] => John 
       )

0 comments:

Post a Comment