I have a form that takes in a users input and puts it in an array, the user then chooses a word they want to find in said array. Thereafter the array is checked to see what each index of the word is as well as each occurrence of the word in the text.
So if you write the string "What is what" and want to find the word "what" it will say that the position of the word is "0 and 2" whereas I'd like it to say "1 and 3". How do I go about this?
Here's the code:
<form action="sida3.php" method="post">
Text: <textarea name="textarea"></textarea>
<br> Search word: <input type="text" name="search">
<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit'])){
$parts = explode(" ", $_POST['textarea']);
$strName = $_POST['search'];
print_r ($parts);
echo '<br>';
foreach($parts as $item) {
if ($item == $strName) {
$counter++;
}
}
echo "The word $strName can be found at: ";
echo implode(' ', array_keys($parts, $strName));
echo "<br>";
echo "The word $strName was found $counter times";
}