I am trying to use similar_text() and in_array() to create a simple in_array() and sentence checker in PHP. I have a text file, dictionary.txt, that is, most of the words in English.
First I put all the words in a text file each on a new line into an array. Then, while entering and introducing the user, I check to see if the entered word is in the array using in_array() . If so, then they write correctly.
If this is not the case, I use similar_text() to search for words in the array that are close to the error word.
I ran into two problems that I could not solve, and I believe that I use in_array() and similar_text() according to the PHP documentation.
The first problem is that when the user enters and sends words that are in a text file and must also be in an array, a fire occurs and this should not happen. Since it is in a text file, it must be in an array, and in_array() must be true.
The second problem is that I get an error that the variable in which I store the percentage of similarity between two words through similar_text() is not defined. I use its similar_text() , as in the examples of documentation comments, in fact, I reinstall and redefine $percentageSimilarity before each comparison. Why am I getting the error that it is not defined?
Here is my code:
<?php function addTo($line){ return $line; } $words = array_map('addTo', file('dictionary.txt')); if(isset($_GET['checkSpelling'])){ $input = (string)$_GET['checkSpelling']; $suggestions = array(); if(in_array($input, $words)){ echo "you spelled the word right!"; } else{ foreach($words as $word){ $percentageSimilarity=0.0; similar_text($input, $word, $percentageSimilarity); if($percentageSimilarity>=95){ array_push($suggestions, $word); } } echo "Looks like you spelled that wrong. Here are some suggestions: \n"; foreach($suggestions as $suggestion){ echo $suggestion; } } } ?> <!Doctype HTMl> <html lang="en"> <head> <meta charset="utf-8" /> <title>Spell Check</title> </head> <body> <form method="get"> <input type="text" name="checkSpelling" autocomplete="off" autofocus /> </form> </body> </html>
source share