There is an array derived from a file with the .csv extension.

$data = file("file.csv"); 

And there is a text received from the input through the global $ _POST array

 $text = $_POST['text']; 

You need to do a search for matching the text with the value in the array. I tried this:

 if (in_array($text, $data)) { echo "Совпадение найдено"; } else { echo "Совпадений не найдено"; } 

For some reason does not find matches. I even tried this:

 foreach ($data as $key => $value) { if($text == $value) { echo "Совпадение найдено"; } else { echo "Совпадений не найдено"; } } 

It still doesn’t find anything (although $ text delivers on purpose identical to one of the array values).

What could be the problem? How to search?

    2 answers 2

    This feature is described in the manual :

    FILE_IGNORE_NEW_LINES is used

    When you call file with the default flags in the array, you will have lines that include newline characters. And from the form the input field goes without a line feed. Accordingly, it is binary different strings and no matches.

    To ensure that line FILE_IGNORE_NEW_LINES are not included in the array elements, there is a separate flag FILE_IGNORE_NEW_LINES . Most likely you do not need empty lines either (you can immediately skip them with the FILE_SKIP_EMPTY_LINES flag), so it turns out

     $data = file("file.csv", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 

      Carefully read the documentation for the file() function used.

      Returns a file as an array. Each array element corresponds to a file string, with newline characters inclusive .

      so you probably compare the string abc with the string of the file abc\n , you end up with the corresponding result. Probably, when searching for the last line of the file, the result will be positive, since there will be no \n character.