PHP Program to count the total number of words in a string

bookmark

<?php
//input string
$str = "The Quick brown fox jumps right over The Lazy Dog";
//counting the words by calling str_word_count function
$response = str_word_count($str);
//printing the result
echo "There are ".$response." Words in <b>".$str."</b>".'<br/>';

//input string
$str = "Hello @ 123 . com";
//counting the words by calling str_word_count function
$response = str_word_count($str);
//printing the result
echo "There are ".$response." Words in <b>".$str."</b>".'<br/>';

//input string
$str = "Hello @ IncludeHelp @ com";
//counting the words by calling str_word_count function
//specify the '@' as a word
$response = str_word_count($str, 0, '@');
//printing the result
echo "There are ".$response." Words in <b>".$str."</b>".'<br/>';
?>

 

Output

There are 10 Words in The Quick brown fox jumps right over The Lazy Dog
There are 2 Words in Hello @ 123 . com
There are 5 Words in Hello @ IncludeHelp @ com