The str_word_count() function in PHP
The php str_word_count() sting function is used to count a number of words in a string.
str_word_count() function syntax
str_word_count(“string”);
PHP str_word_count() function example
<?php
echo "The number of words in a string = ". str_word_count("Hello Meera Academy");
?>
In above example there are three words in a given string the “Hello”, “Meera” and “Academy”.
The output is:
The number of words in a string = 3
PHP str_word_count() example with php form
<html>
<head>
<title>PHP str_word_count() Function</title>
</head>
<body>
<FORM method="POST" action="firstexample.php">
Name : <input type="text" name="name"></br></br>
<input type="submit" name="Submit1" value="Word count">
</FORM>
<?php
if(isset($_POST['Submit1']))
{
echo "The number of words in a string = ". str_word_count($_POST["name"]);
}
?>
</body>
</html>
The out put is :