How can PHP's str_word_count function be effectively utilized to count words in a text string?

To count words in a text string using PHP's str_word_count function, you can simply pass the text string as the first parameter to the function. This function will return the number of words found in the string. It also has optional parameters that allow you to specify whether to include numbers as words and to return an array of words found in the string.

$text = "This is a sample text string.";
$word_count = str_word_count($text);
echo "Number of words in the text: " . $word_count;