How to find a link within an html file and switch to another link?

Asked

Viewed 747 times

6

So far I could only know if part of the link exists in the file, but I do not know how to make the exchange. My code and this:

$nomeArquivo = $_POST['nomeArquivo'];


foreach ($arquivos as $a){
$aq = fopen($a,"r+");
while (!feof($aq)){
    $le = fgets($aq);

    if(preg_match("/arquivo.txt/",$le)){

        //realizar troca
       //salvar arquivo
    }

}
fclose($aq);
}
  • Could you be more clear in your doubt? I don’t understand

  • I saw that you are a new user, so don’t take this personally, but consider changing or detailing the functions/variables. It’s extremely exhausting to be looking for what would be $le, $a, $aq and things that are never used, for example $nomeArquivo, which confuses me even more. In a next post consider giving more details, essentially within the code itself, with small changes, so you can get even better and faster answers. :)

  • Thanks for the tips, I’ll do it next time . thank you very much for the willingness to help me.

2 answers

2


To perform the substitution you can use the str_replace or preg_replace.

In this case just change to:

<?php
//...
    if(preg_match("/arquivo.txt/",$le)){

    $conteudo = str_replace("arquivo.txt", "novo_arquivo.txt", $le);
    // Alternativa: preg_replace("/\barquivo.txt\b/", "novo_arquivo.txt", $le);
    // Substitui o arquivo.txt por novo_arquivo.txt
    fwrite($aq, $conteudo);
    // Escreve o conteúdo editado.

    }
//..
?>

Optimal solution:

Abandon the fopen, will be easier and will not have so much loss of performance.

<?php
//...

foreach ($arquivos as $caminho){

$conteudoArquivo = file_get_contents($caminho);
// Isso irá pegar todo o conteudo do arquivo

  if(preg_match("/arquivo.txt/", $conteudoArquivo)){
  // Se existir arquivo.txt

  $conteudoEditado = str_replace("arquivo.txt", "novo_arquivo.txt", $conteudoArquivo);
  // Novo $conteudoEditado terá a alteração

  file_put_contents($caminho, $conteudoEditado);
  // Salva as alterações
  }

}
//..
?>

Important (function feof()):

There are some cases where the feof() can create an infinite loop, even there are examples in the PHP documentation, which can be accessed clicking here.

  • I built the code, but it’s giving this error :Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in /var/www/php/test.php on line 7 vc know what it might be ? . I put all the code on this site https://codeshare.io/ZQg7c

  • Replace with str_replace.

  • If you use the preg_replace use preg_replace("/ barquivo.txt b/", "novo_file.txt", $le), note the /\b_______\b/ to restrict the word. Sorry for the error in the function. :[

  • Dude, now it worked out, only it keeps the old link and on the line below it overwrites correctly, and after overwriting everything that existed below the file it erases , would have some way to keep the logs below and it only delete the old link ?

  • This happens because fgets reads line by line. I will rewrite to this situation.

  • I edited the publication. ;]

  • Dear, God bless you, thank you very much for your attention

Show 2 more comments

0

It is not clear because depending on what needs to be done there are various ways to solve.

For example, if you just want to exchange a specific string on all links, you can use str_replace() and save with file_put_contents(). I wouldn’t even have to open with fopen() and seek with regex. But like I said, it depends on what really needs to be done.

For example, if you just want to replace one string with another within the contents of a file:

file_put_contents(str_replace('nome-antigo.txt', 'nome-novo.txt', file_get_contents($caminho_do_arquivo)));
  • Which is more recommended, because I want to create an upload of files , and when I send this file the name of the file will replace on the page the name of the old file ,so I’m trying to do this with variables for now , to then do this other part

  • is very confusing.. So it has nothing to do with reading content from the file. What you want is to rename the files. That’s it?

  • Cara has yes , and where I work we use a very old intranet , and in a university , we have a download page of theses manuals , this same link is available in several pages of the system , what I want to do and when to upload a new manual , this same link is updated in the other automatic pages. as the system is old to not touch the other pages when we post a new thesis . we have replaced the old file with the new one, but we are having problems with caching in the clients' browsers

  • So you need to read all the html files on this intranet site and replace the old links with the new link. Is that it? Leave this more detailed in the question.

  • Yes and that’s right ,to get an idea a single manual is available in 11 different pages , I want to pass the specific pages or the directory and the script make this change of the links . Thanks to the help of vcs ,I’m almost getting , I’ve set up the form where I put the file name and the script searches this link on another page ?

  • describe these details in the question...

Show 1 more comment

Browser other questions tagged

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