Solved

search and replace with html files

Posted on 2004-09-01
1
201 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
MySQL limit and not so limited 13 41
Add Logo to Bookmark and Tab Headings in Browser 3 32
xml with php question 5 37
How do I post more than 1 item to php backend 24 31
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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