str_word_count() function in php
0 2359
str_word_count() function of PHP is used to count the number of words present in the PHP string. It is an inbuilt function of PHP.
Syntax:
here,
1 $string is a PHP string which words to be counted by this function. It is required.
2 $return_val decides the return type of this function. It is an optional parameter. It has 3 possible values.
a. 0: This is the default value of this parameter. The function returns the total number of words present in the string.
b. 1: The function returns an indexed array that contains the words present in the string.
c. 2: The function returns an associative array which key is the position of the word in the string and value is the word.
3 $chars represent some special characters which to be counted as words. It is an optional parameter.
Example:
<?php $str="Welcome to Coding Tag"; $res1=str_word_count($str,0); // result with return parameter 0 $res2=str_word_count($str,1); // result with return parameter 1 $res3=str_word_count($str,2); // result with return parameter 2 echo "<pre>"; echo "Number of words is :<strong>$res1</strong><br>"; echo "Total words are:<br>"; print_r($res2); echo "<br>Total words with their positions are:<br>"; print_r($res3); ?>
Output:
Share:
Comments
Waiting for your comments