str_word_count 


string apg

RETURN the information about number of words used in a STRING.





This function counts the number of words inside $string.

If the optional $format is not specified, then the return value will be an integer representing the number of words found. In the event the $format is specified, the return value will be an array, content of which is dependent on the $format.

This function is locale dependent.



<?php

mix str_word_count 
str $string [, 
                                  
int $format [, 
                                  
str $charlist ]] )


where,

$string The STRING for word count

$format 
To control how the STRING will be examined
                
SEE the below TABLE )
             
$charlist list of additional characters 
                            which will be considered 
as word

?>
 

$string


The input STRING to be examined.



$format


To control how the STRING will be examined.

The possible value for the $format and the resultant outputs are listed below.

VALUE WHAT RETURN
0 the number of words found
1 an array containing all the words found inside the $string
2 an associative array, where the key is the
numeric position of the word inside the $string
and the value is the actual word itself
ed48


$charlist


A list of additional characters which will be considered as 'word'



  1 EXERCISE   

<?php

$txt01 
"The quick brown fox jumps over the lazy dog.";

$cnt01 str_word_count($txt01);

echo 
$txt01 '<br>[ ' $cnt01 ' words ]<br>';

?>

 RESULT   

The quick brown fox jumps over the lazy dog.

[ 9 words ]


  2 EXERCISE   

<?php

$txt02 
"The quick brown fox jumps over the lazy dog.";

$cnt01 str_word_count($txt02);
$cnt02 str_word_count($txt021);

echo 
$txt02 '<br>[ ' $cnt01 ' words ]<br><br>';

foreach(
$cnt02 as $cn02 => $ct02)
{
echo 
$cn02 ' - ' $ct02 '<br>';
}

?>

 RESULT   

The quick brown fox jumps over the lazy dog.

[ 9 words ]

0 - The
1 - quick
2 - brown
3 - fox
4 - jumps
5 - over
6 - the
7 - lazy
8 - dog


  3 EXERCISE   

<?php

$txt03 
"The quick brown fox jumps over the lazy dog.";

$cnt01 str_word_count($txt03);
$cnt03 str_word_count($txt032);

echo 
$txt03 '<br>[ ' $cnt01 ' words ]<br><br>';

foreach(
$cnt03 as $cn03 => $ct03)
{
echo 
'At position ' $cn03 ' we have: "' $ct03 '"<br>';
}

?>

  4 EXERCISE   

<?php

$a 
"";

var_dump(str_word_count($a));

try {
    
var_dump(str_word_count($a, -1));
} catch (
ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

try {
    
var_dump(str_word_count($a3$a));
} catch (
ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

var_dump($a);

?>

  5 EXERCISE   

<?php

$str 
"Hello friend, you're
    looking          good today!"
;
    
$b =& $str;

var_dump(str_word_count($str1));
echo 
'<br><br>';
var_dump(str_word_count($str2));
echo 
'<br><br>';
var_dump(str_word_count($str));
echo 
'<br><br>';

try {
    
var_dump(str_word_count($str3));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

try {
    
var_dump(str_word_count($str123));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

try {
    
var_dump(str_word_count($str, -1));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

try {
    
var_dump(str_word_count($str999999999));
} catch (
\ValueError $e) {
    echo 
$e->getMessage() . "<br><br>";
}

var_dump($str);

$str2 "F0o B4r 1s bar foo";

var_dump(str_word_count($str2NULL"04"));
echo 
'<br><br>';
var_dump(str_word_count($str2NULL"01"));
echo 
'<br><br>';
var_dump(str_word_count($str2NULL"014"));
echo 
'<br><br>';
var_dump(str_word_count($str2NULL""));
echo 
'<br><br>';
var_dump(str_word_count($str21"04"));
echo 
'<br><br>';
var_dump(str_word_count($str21"01"));
echo 
'<br><br>';
var_dump(str_word_count($str21"014"));
echo 
'<br><br>';
var_dump(str_word_count($str21""));
echo 
'<br><br>';
var_dump(str_word_count($str22"04"));
echo 
'<br><br>';
var_dump(str_word_count($str22"01"));
echo 
'<br><br>';
var_dump(str_word_count($str22"014"));
echo 
'<br><br>';
var_dump(str_word_count($str22""));
echo 
'<br><br>';
var_dump(str_word_count("foo'0 bar-0var"2"0"));
echo 
'<br><br>';
var_dump(str_word_count("'foo'"2));
echo 
'<br><br>';
var_dump(str_word_count("'foo'"2"'"));
echo 
'<br><br>';
var_dump(str_word_count("-foo-"2));
echo 
'<br><br>';
var_dump(str_word_count("-foo-"2"-"));

?>