Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

search and replace with html files

Posted on 2004-09-01
1
Medium Priority
?
219 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 1500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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 …
Suggested Courses

636 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