Solved

search and replace with html files

Posted on 2004-09-01
1
185 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

809 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