Solved

search and replace with html files

Posted on 2004-09-01
1
208 Views
Last Modified: 2006-11-17
hi there

I am looking for away of searching from a list of urls and replacing them with a second batch of urls within a collection of html files around 75 html files.(same folder)

Example

so all line by line domains that in the first text file are searched for and replaced by the second line by line domains

first text file could have this

www.domain1.com/?=111
www.domain2.net/?=3435
www.domain3.com/?=1411
www.domain4.net/?=3445
www.domain5.com/?=43111
www.domain6.net/?=23345
etc etc

there are about 50 urls all within the html files in the first textfile so perfect matches through out.

Then i need to replace those urls within the html with the second text file (line by line)

www.domain1.com/?=5435
www.domain2.net/?=347835
www.domain3.com/?=16411
www.domain5.com/?=4356111
www.domain6.net/?=233745

both files are different in the amount of urls the only thing that is identical are the domain names up until the http://www.domain.whatever/ after that the seconf text file has a different ?=XXXX

Then output the master files into another folder

I dont think this is as stright forward as text to text then master text file

best regards



0
Comment
Question by:playstat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 36

Accepted Solution

by:
Zyloch earned 500 total points
ID: 11959435
Hi

That could be a bit tricky. This example won't allow for Javascript links such as window.location changes but only for <a href>, are you asking for something like this:?

<?php
//Set files
$firstFile = "file1.html";
$secondFile = "file2.html";

//Open first file and get contents
$f1=fopen($firstFile,"r");
$contents=fread($f1,filesize($f1));
fclose($f1);

//Grab links
preg_match_all("/<a\s+[^>]*?href=([\'\"]{0,1})(.+)?\\1.*?>/",$contents,$matches);

//$matches[2] should be the array of links
//Open 2nd file
$f2=fopen($secondFile,"r+");
$contents=fread($f2,filesize($f2));

//Replace all URLs
foreach ($matches[2] as $url) {
   $contents=preg_replace("/<a(\s+[^>]*?href=)([\'\"]{0,1})(.+)?\\2(.*)?>/","<a\${1}\"${url}\"\${4}>",$contents,1);
}

reset($f2);
fwrite($f2,$contents);
echo("Done!");

?>

 
This hasn't been error checked yet and might have some errors, but give it a try.

Regards,
Zyloch
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question