0

I need a function to compare two text files and find the similarities between them. I have tried using "Strpos" and "similartext", but neither of them seem not to working. I am not sure how to compare the two files.

Here is my code which has two files text one that been track the keystrokes from the user and other is list of words that be compare to another file This code is designed to track the keystrokes of the user and compare them to a list of words. If the keystrokes match a word on the list, then the code will output the echo message otherwise nothing.



echo "<h3> <center>" . '#####Alert#####';
$text = "<br> Unsafe user Found";

$String = 'file.txt';
$list = 'wordlist.txt';





 //compare the difference between the string and the list of word 


    
for ($i = 0; $i <= 0; $i++){
   
    if (strpos(file_get_contents ($String , $list) )!== false) {
            echo  $text;
            //mail($to, $subject, $message);
        }
      return false;


}

For example, if the user typed account it should echo message because the account word is on the list. However, if the user typed in a word that is not on the list, the program would not be able to echo a message.

Please accept my apologies for any errors

Roger31
  • 3
  • 1
  • 3
  • I think you are not comparing two text files , but check the contents of a text file and see whether it contains one of the words in another text file (wordlist.txt) ? – Ken Lee Dec 23 '22 at 08:49
  • Yes, that is what I meant. It checks whether a hack word exists in the text file by looking for the same word or other words. – Roger31 Dec 23 '22 at 09:00
  • Read documentation about [file_get_contents()](https://www.php.net/file_get_contents). Also, what is the purpose of `for ($i = 0; $i <= 0; $i++){`? It does only 1 iteration. Why `return false`. This code is inside a function? – Syscall Dec 23 '22 at 09:22
  • If `wordlist.txt` is the file containing a list of words to check against user input then `in_array` could be used with the array being generated by using `file(wordlist.txt)` etc - but it is not clear how the words in `file.txt` are added to that file nor the contents of it. Perhaps you can add more detail like example contents from each file and the form used? – Professor Abronsius Dec 23 '22 at 09:24

1 Answers1

0

To compare the contents of two txt files and find the similarities between them,Read the contents of both files into separate variables using the file_get_contents() function. Split the contents of each file into arrays of words using the explode() function. compare the words using the in_array() function. If a word from one array is found in the other array, you can output the string or take any other desired action.

echo "<h3> <center>" . '#####Alert#####';
$text = "<br> Unsafe user Found";

$String = 'file.txt';
$list = 'wordlist.txt';

// Read the contents of both files into separate variables
$stringContents = file_get_contents($String);
$listContents = file_get_contents($list);

// Split the contents of each file into arrays of words
$stringWords = explode(' ', $stringContents);
$listWords = explode(' ', $listContents);

// compare the words using `in_array() function`
foreach ($stringWords as $word) {
    if (in_array($word, $listWords)) {
        echo $text;
    }
}
Ali Usama
  • 100
  • 1
  • 7
  • 1
    This will work. I'm wondering about the performance as the files grow larger though. `array_intersect` would be significantly faster, just do `array_intersect($stringWords, $listWords)` instead of `foreach ... in_array`. See: https://stackoverflow.com/questions/40331407/php-in-array-vs-array-intersect-performance ... and again, `array_flip` and `array_intersect_key` would be faster still. – Markus AO Dec 23 '22 at 10:42