Php like_text () and in_array () don't work, as it seems like they should

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> 
+4
source share
2 answers

Change your add to string to

 function addTo($line){ return strtolower(trim($line)); } 

and change the input to

 $input = strtolower(trim($_GET['checkSpelling'])); 

The file command has the unpleasant habit of leaving the trailing newline character, so you probably disagree with this ... trimming should take care of this. The rest of the changes are here to make the case insensitive.

+2
source

When you use file() , each $words element will still have a newline attached to it. You can delete it using FILE_IGNORE_NEW_LINES :

 $words = file('dictionary.txt', FILE_IGNORE_NEW_LINES); 

You can also normalize the needle by applying strtolower() , assuming that all dictionary entries already have lowercase letters:

 if (!($input = filter_input(INPUT_GET, 'checkSpelling', FILTER_UNSAFE_RAW))) { die("Bad input, probably"); } $input = strtolower($input); 

This is because in_array() not case insensitive; for example, "Hello" != "hello" .

Further normalization may include the removal of something non-verbal from your words.

+1
source

Source: https://habr.com/ru/post/1484330/


All Articles