Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
elimine una línea (.txt-File) que contenga algo - con php

Quiero eliminar una línea que contiene algo proporcionado por $_POST['link']

Ya funciona con el archivo .csv. En este punto, el código del archivo .txt no hace nada.

mi codigo php

 //txt $txt = file('../textFiles/websites.txt'); $fpTXT = fopen('../textFiles/websites.txt', 'w'); foreach ($txt as $lines) { if (!str_contains($lines[2], strval($_POST['link']))) { fwrite($fpTXT, $lines); } } fclose($fpTXT);

el gatillo con js

 <!-- Delete-Function --> <script> async function deleteRequest(elem) { $.ajax({ type: 'POST', url: '/sites/deleting.php', dataType: 'html', data: { 'link': elem } }); location.reload(); }; </script> </head> <!-- Delete Icon --> <?php $delBtn = "<td><button class='delete' onclick=\"deleteRequest(this.parentNode.parentNode.getElementsByTagName('td')[2].innerText)\"><i class='material-icons'>&#xE872;</i></button></td>"; ?>

código para el csv (encima del código txt)

 //CSV $csv = array_map('str_getcsv', file('../textFiles/websites.csv')); $fpCSV = fopen('../textFiles/websites.csv', 'w'); foreach ($csv as $fields) { if (!str_contains($fields[2], strval($_POST['link']))) { fputcsv($fpCSV, $fields); } } fclose($fpCSV);

2 líneas del archivo .txt

ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Las "$líneas" que obtiene del file() no son lo mismo que un registro CSV. Cada uno de ellos es una sola línea, por lo que $lines[2] se refiere al tercer carácter de esa línea:

 $lines = "https://www.google.com\n"; $lines[2] is just a "t".

Mientras esté en un registro CSV, tendría

 $lines = [ "A1", "B1", "C1", "D1" ] and here, $lines[2] is "C1".

Debe filtrar las líneas de texto para eliminar la que no desea

 $outputFile = '../textFiles/websites.txt'; $remove = strval($_POST['link']); $txt = array_filter( file($outputFile), function ($eachLine) use ($remove) { return !str_contains($eachLine, $remove); // or equivalently, // return false === strpos($eachLine, $remove) // In all cases, consider that if someone sends ".", // you will remove ALL sites, since they'll all have a dot in their site name. // So maybe is better // return $remove !== trim($eachLine) // (the "trim" is needed to get the carriage return at the end of line out of the comparison, which otherwise would always fail). } ); // Now you need to write the cleaned file file_put_contents($outputFile, implode('', $txt));
over 4 years ago · Santiago Trujillo Relatório

0

Si solo desea reemplazar una palabra o una oración del archivo, puede hacerlo fácilmente con str_replace :

 $file = '../textFiles/websites.txt'; $findText = $_POST['link']; file_put_contents($file, str_replace($findText, '', file_get_contents($file)));
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda