How to remove a phrase within a TXT file

Asked

Viewed 219 times

-1

Say hello to the boys, I’m needing a help, I want to remove a KEY inside a TXT file but I’m not getting.

Keys.txt:

aLZkGWPXTLVdpVl
w17AzQZIy22soy9
rAcEiWHwopYz2Pk
uKPhbc0Xok5a2aO

my code . php:

<?php
    $texto = "w17AzQZIy22soy9";

    $linha_n = $texto;
    echo $linha_n;

    //Ler o arquivo
    $linhas = explode("\r\n", file_get_contents("youtube.txt"));

    // Abre o arquivo e coloca o ponteiro no final
    $arquivo = fopen('keys.txt','r+');
    if ($arquivo) {
        while(true) {
        $linha = fgets($arquivo);
        if ($linha==null) break;


    // Busca o conteudo que vai ser alterado
    if(preg_match("/$linha_n/", $linha)) {
    $string =+ str_replace("$linha_n", "", $linha);
    } else {
    // Vai colocando tudo numa nova string
    $string =+ $linha;
    }
    }
    // Move o ponteiro para o inicio pois o ftruncate() nao fara isso
    rewind($arquivo);

    // Apagar todo o conteudo
    ftruncate($arquivo, 0);

    // reescreve o conteudo do arquivo
    if (!fwrite($arquivo, $string)) die('Não foi possível atualizar o arquivo.');
    echo 'Arquivo atualizado com sucesso';
    fclose($arquivo);
    }

?> 

Could you guys help me out? Bs: I have already researched here in the community and in other forums and have not found the solution!

  • Important you [Dit] your question by reducing the code to a [mcve] problem with the solution attempt. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

1 answer

2


Commented code.

$search = "w17AzQZIy22soy9";
$filename="keys.txt";
$contents = file_get_contents($filename);
$new_contents = "";
if (strpos($contents, $search) !== false) { // se o arquivo contem $search
    $contents_array = explode(PHP_EOL, $contents);
    foreach ($contents_array as &$record) {    // para cada linha
        if (strpos($record, $search) !== false) { // se encontramos a linha correta
            continue; // encontramos a linha a ser excluída - portanto, não adicionaremos ao novo conteúdo.
        } else {
            $new_contents .= $record . "\r"; // não é a linha que queremos excluir, então a mantemos
        }
    }
    $new_contents=trim($new_contents);
    file_put_contents($filename, $new_contents); // salve os registros no arquivo
    echo json_encode("Arquivo atualizado com sucesso!");
}
else {
    echo json_encode($search ." não existe no arquivo!");
}

PHP_EOL - Available from PHP 5.0.2

Older versions $contents_array = explode("\n", $contents);

  • @Gustavoalves, you tested what I suggested in the comment above?

  • @Gustavoalves, but I saw the txt file on my server and the line was deleted.

  • 1

    reply accepted means success. I withdraw comments with links

  • final result: I managed to fix, the error itself was the VPS that limited file editing, I bought another VPS... Thanks for helping!

Browser other questions tagged

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