In the list.txt file, I store the data like this:

я ты мы вы 

and choose the answer for the GET parameter

  $list = file_get_contents('list.txt'); $exp = explode($_GET['r'], $list); print_r($exp); 

For example, if $_GET['r'] will have the value "I", then in $exp[1] should be "you" but in the place of it there is "you and you"

  • Everything returns you correctly, because in $list you have all the text, and not one term is found. - Visman

2 answers 2

try this:

 <?php // загружаем файл в массив построчно $file = file('list.txt'); // пробегаем по массиву foreach ($file as $file_row) { // разбиваем строку по символу табуляции на пару значений $file_val = explode("\t", $file_row); // если первое значение совпадает с переданным методом GET if ($file_val[0] == $_GET['r']) { // выводим второе значение без правых пробельных символов echo rtrim($file_val[1]); // останавливаем пробег по массиву break; } } ?> 

At the same time in the text document as a separator between the pair make a tab character.

  • Nothing deduces - viktor
  • It may not display if the file does not find a value from GET (in particular, by case). Or if you have not made a tab character as a delimiter in a text document. I see no other reasons. - Dmitriy

Everything solved the problem, Dmitry helped :)

 $file = file('list.txt'); foreach ($file as $file_row) { $file_val = explode($_GET['r'], $file_row); echo $file_val[1]; } 
  • one
    I would add the condition for stopping the run and the rtrim function for deleting the line feed character from the output value, - Dmitry
  • In general, this is the wrong code. Hone my task. You will output at the first occurrence of a variable from GET to a string. If you use the word GET with the help of GET, and in the file there is the line "the earth is full of degenerates", then the page will show off the phrase "full of degenerates". - Dmitriy