PHP Read TXT, delete lines, create new file

Asked

Viewed 1,439 times

0

I want to use PHP and xamp/wamp to do the following task on my computer without the need to upload/download:

I have dozens of TXT files on the computer, each TXT containing only 1 name per line, no strokes, dots, etc.

I want to do a search, for example, for all people with surname "Silva" in these dozens of files, so PHP would read all TXT files, move all names with "Silva" to silva.txt and save the 2 files.

So far they have explained the following: The stop is to take the files and pass in php by a foreach on each line, add in the database, but before add do a search if there is already the data in the new file, then save the 2 files.

Can someone give me a light on how to do this?

I confess that I do not understand almost anything of PHP and I am beating head p/ do this foreach in several files at the same time, create this BD, create a new file, move everything, see if it has repeated, put in alphabetical order and save all the files later.

It’s complicated to work....

  • Post the code of what you have tried so far. And in what part of the code you have doubts, so it is easier to clarify the doubts.

  • If the files are in a single folder, it’s easier... Are??

  • They are all in the same folder. Find this here to read the files. Processing all files in a directory <? php $dh = dir ("/home/1www/"); while ($input = $dh->read()) { print $input . " <br>"; } $dh->close(); ?>

  • To open the write file I found this: <?php $Fp = fopen("./data.txt", "w"); while (!feof($Fp)){ $char .= fgetc($Fp); } fclose($Fp); echo $char." <br><br>"; ?>

  • <?php $file = file("list.txt"); foreach ($file as $i => $value) { } usort($files, "porData");

  • I’m trying to put all these parts together and make it work.

  • The files are simple: 001.txt, 005.txt, they have only one name per line, then ENTER, all TXT and in the same folder.

Show 2 more comments

1 answer

0


$base = __DIR__.DIRECTORY_SEPARATOR; // Diretório onde estão os arquivos txt.
$files = glob($base.'*.txt'); // Pega todos os arquivos que terminam com .txt

$search = 'silva'; // a palavra que deseja buscar
$found = array();
$arr = array();
// Itera os arquivos encontrados
foreach ($files as $file) {

    // Lê cada arquivo em um array
    $arr = file($file);
    foreach ($arr as $k => $v) {
        // Se encontrar a palavra, guarda no array $found e remove do array que leu o arquivo.
        if (stripos($v, $search) !== false) {
            $found[] = trim($arr[$k]);
            unset($arr[$k]);
        }
    }
    // Salva os dados no arquivo, com os nomes removidos
    if (!empty($found)) {
        file_put_contents($file, implode('', $arr));
    }

}
// salva todos os nomes encontrados
if (!empty($found)) {
    file_put_contents($base.'silva.txt', implode(PHP_EOL, $found));
}

unset($found, $arr);
  • Hi Daniel, installed the Xampp again and worked all legal.

  • I really appreciate your help, I’ll have hours of work to do here but it would be months if you hadn’t helped.

Browser other questions tagged

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