Solved

search and replace with html files

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now