The
str_word_count() function will let you know how many words are in a string you pass through it. You can use one or two parameters in this function. We explain the parameter use in the code below.

<?php
$myString = "Programmers are dorks!";
// Return the number of words by using "0" or supply no second parameter
$count = str_word_count($myString, 0);
echo $count."<br />";
// Return a normal array with all of the words in it by using "1"
$countArray = str_word_count($myString, 1);
echo serialize($countArray)."<br />";
// Return an associative array by using "2" as the second parameter
$countArray = str_word_count($myString, 2);
echo serialize($countArray)."<br />";
?>
3
a:3:{i:0;s:11:"Programmers";i:1;s:3:"are";i:2;s:5:"dorks";}
a:3:{i:0;s:11:"Programmers";i:12;s:3:"are";i:16;s:5:"dorks";}