Search a Input word in a TXT and find words that are in the same line in PHP

Asked

Viewed 92 times

-3

Example of TXT file:

John;1

Peter;2

Marlon;3

Luke;4

etc....

By typing only the name in the Input I need that too the user ID appears,

Example of Input: Marlon

and when you type only name in Input, result in Echo or in

same. turn up:

Name: Marlon

ID: 3

If you can be very specific about the code, because I’m a beginner...

  • It is very complicated to understand your question but it seems that you are looking for something like a complete auto ajax

1 answer

2

I would recommend that you work with a file in json format that is much more intuitive, at least in my opinion.

You can do the following:

$json = fopen("caminho do arquivo" . $nomedoseuarquivo . ".json", "w+"); 

//você cria um array na estrutura json
$meujson = array(
'ID'   => '1',
'nome'     => 'William',
);

//encoda o array criado anteriormente e deposita no arquivo  
fwrite($json, json_encode(meu_json));
fclose($json);

And to get this data just use the function json_decode

 //a função file_get_contents pega todo o conteúdo do seu arquivo e deposita na variável file
 $file = file_get_contents("caminho do seu arquivo" . nome do seu arquivo .".json");
 //decodo o conteudo de file e deposito na variável json 
 $json = json_decode($file);

From there the variable $json will be loading the contents of your file and just you fetch the elements you want , a tip is you use the function in_array.

But if you want to opt for the traditional way, I built an example of logic that you can follow:

<?php

 $nome='oi';//Seria o conteúdo que está vindo do seu post

 //ESTOU LEVANDO EM CONSIDERAÇÃO QUE SEU ARQUIVO VAI ESTAR NA ESTRUTURA  
 //id-NOME E ACONSELHO QUE VOCÊ FAÇA O MESMO)
 $teste = array('2-oi','3-THAU'); //estou simulando oque o file_get_contents 
 //te retornaria

//Esse foreach vai percorrer toda a string que o file_get_contets retorna
foreach($teste as $x)
{

  //pego o id na minha string
  $id = strstr($x,"-",true);

  //echo $id;

  //retiro o id que coletei anteriormente da variável $teste e deposito o resultado na variável $aux
  $aux = str_replace($id,"",$teste);

  //Retiro agora o - que ficou em $aux e deposito em $aux2
  $aux2 = str_replace('-',"",$aux);

  //para vizualizar você não pode usar o echo pois se trata de uma string e não array
//print_r($aux2) ;

//Como se trata de uma string tenho que pegar uma posição, no caso estou verificando se o conteúdo de $nome existe no arquivo, se existir mostro o seu id
  if ($aux2[0] == $nome)
  {
     echo "elemento encontrado :) seu é id :$id";
  }
  else
  {
    echo 'erro';
   }

}

From one searched in each function I used so you also learn how to use the facilities of PHP and if you want to test this excerpt I created just play it on this site http://phptester.net/

Links for you to take a deeper look:

in_array: http://php.net/manual/en/function.in-array.php

json_decode : http://php.net/manual/en/function.json-decode.php

json_encode : http://php.net/manual/en/function.json-encode.php

How to work with PHP files : http://php.net/manual/en/function.fopen.php

str_replace:https://www.w3schools.com/php/showphp.asp?filename=demo_func_string_str_replace

strstr:http://php.net/manual/en/function.strstr.php

  • calm became a mess what I said, I will put in a place the code that I possess...

  • https://pastebin.com/raw/sTur7WJ7

  • Take a look at my main answer, I have developed a code that will help solve your problem

  • http://prntscr.com/l4vo9c http://prntscr.com/l4vpln&#Xa..

  • is for you to adapt your code with what I’ve given you

  • Yeah but I don’t know... I’m kind of super beginner in php...

  • of a force please..

  • @Kelvynserrão , this forum does not aim to simply make algorithms when someone asks, but to help in specific problems or doubts so much that only I gave attention to your question. If you want to join this area by own experience go after , because you will not always find the answer you need and in addition PHP has a super complete online documentation in my view 1000X better than java.

  • If you like video lessons I recommend the "COURSE IN VIDEO" which has a full course of PHP 100% free link:https://www.youtube.com/watch?v=F7KzJ7e6EAc&list=PLHz_AreHm4dm4beCCCmW4xwpmLf6EHY9k

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.