I need to count how many times a user entered word appears in an array. The user enters text into a form textbox, and that text goes to a String variable in php, which is then exploded into an array with an index for every space.
I need to count how many times each word appears. Let's say the text is "yo yo yo man man man", then I need to count how many times the word "yo" appears as well as the amount of times "man" appears. Please keep in mind this text could be anything the user enters, so using a method such "str_word_count" isn't option, since it only takes hard-coded text.
The code I have set up so far:
form.php:
<html>
<head>
<title>Part 1</title>
</head>
<body>
<h1></h1>
<form action="part1result.php" method = "post">
<input type="text" name="text"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
result.php:
<head>
<title></title>
</head>
<body>
The text you entered was:
<?php
$text = $_POST['text']; // get text
$textToCount = explode(' ', $text);
echo $text.'<br><br>';
for($i=0; $i<count($textToCount); $i++)
{
}
?>
</body>