2

Currently looking tot speed up a manual task.

I have several web URLS in a .txt file that I'm hoping to add to a line of code for each URL.

The code is:

<tr>
    <td><a href=""></a></td>
</tr>

Basically I would like to automate my batch file to add the URL in both between the commas"" and <> to then give an output file such as:

<tr>
    <td><a href="http://www.test1.com/">http://www.test1.com/</a></td>
</tr>
Joeri
  • 2,214
  • 24
  • 24
hayd
  • 21
  • 1
  • 2
    This is very simple to do, and I'm sure you can figure out yourself if you know what to look for, and learn something from it ;) What you are trying to do is to walk over the lines in a text file, one by one, and then to append a certain String to a different file. Each string you add is simply the first HTML snippet you showed, with the contents of the line of the first file printed between the apostrophes. – Erik S May 12 '15 at 09:54
  • Hi, thanks for the post but not sure exactly on which commands I need could you point me in the right direction? – hayd May 12 '15 at 11:31
  • `for /f` loops are used for going line by line through a text file. – SomethingDark May 12 '15 at 12:48
  • [This is a post about reading a file line by line](http://stackoverflow.com/questions/13246597/how-to-read-a-file-line-by-line-in-php). Inside of the while loop, you will want to write a line each iteration. [You can use this function to do so](http://php.net/manual/en/function.file-put-contents.php). If you don't understand while loops, I humbly suggest you try out a PHP tutorial (if you want to learn, that is!). Otherwise, you're better off going somewhere where you can request scripts ;) – Erik S May 12 '15 at 13:10

1 Answers1

0
  1. Read the file.
  2. Explode into an Array
  3. Loop with foreach through the Array
  4. String replace a stub with the found values.

<?php

$stub  = '<tr><td><a href="%__link__%">%__link__%</a><td></tr>';
$links = [];

$filedata = file_get_contents("filename.txt");
$lines = explode( "\n", $filedata ); // \n for unix, \r for windows.

foreach( $lines as $line )
{
    $links[] = str_replace( "%__link__%", $line, $stub );
}

$newfile = join("\n", $links);

file_put_contents("filename.html", $newfile);
Joeri
  • 2,214
  • 24
  • 24
  • Hi thanks for the response, would this work within just a .bat file? – hayd May 13 '15 at 13:37
  • Do you mean in a .bat file that starts a .php file? Just run the .php file from cmd. http://php.net/manual/en/install.windows.commandline.php – Joeri May 13 '15 at 13:53
  • But... if you already have a list of url's then it's not much work to add if you have the right editor... take a look at this: http://www.sublimetext.com/ – Joeri May 13 '15 at 13:58