Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

processing html file

Status
Not open for further replies.

jin

Programmer
Aug 2, 2001
3
0
0
SG
i posted this on a different forum, but it's probably more appropriate here:

I have a short project...basically, I have a HashMap with several words in it. I also have a input html file with some text in it.

My project is that I have to make a new file...which has the same contents has the input file, BUT if any of the words from the Map shows up in the inputfile, I have to make those words hyperlinks.

For example, if the word "computer" is in the hashmap...and "The computer has 128 megs of RAM" is in the input.html file...i would create a new html file that says "The computer has 128 megs of RAM" -- BUT here 'computer' would be a hyperlink. (Thus, in the end, I would have a new HTML file which would have links to all words in the hashmap.)

I realllllllllllly need help here. I bet to many of you this is a simple problem!

( In the hashmap the key is the word, and the value is the hyperlink code. )

-Jin
jinen@hiphopsite.com

 
Hello,
I don't know the format of your hash table.
I'll suggest a client-side solution.
You will need 3 files:
input.js - with input html inside.
hashmap.js - with an assosiative array
output.html - with transformation function

input.js
===============
str = &quot;<HTML>\n&quot;
str += &quot;<HEAD>\n&quot;
str += &quot;<TITLE> Input file </TITLE>\n&quot;
str += &quot;</HEAD>\n&quot;
str += &quot;<BODY>\n&quot;
str += &quot;There is a computer.<br>\n&quot;
str += &quot;Search Yahoo.\n&quot;
str += &quot;Search Google.\n&quot;
str += &quot;</BODY>\n&quot;
str += &quot;</HTML>\n&quot;

hashmap.js
==============
hashmap = new Array();
hashmap['Yahoo']='hashmap['Google']='hashmap['computer']='
output.html
==============

<HTML>
<HEAD>
<TITLE> HashMap </TITLE>
<script language=&quot;JavaScript&quot; src=&quot;hashmap.js&quot;></script>
<script language=&quot;JavaScript&quot; src=&quot;input.js&quot;></script>
<script language=&quot;JavaScript&quot;>
function transform(){
for (ind in hashmap) { // ind is index from hashmap
if (str.indexOf(ind)!=-1) { // there is an ind substring in input file
lnk = &quot;<a href='&quot; + hashmap[ind] + &quot;'>&quot; + ind + &quot;</a>&quot;;
str = replaceString(ind,lnk,str);
}
}
document.write(str);
}
function replaceString(oldS,newS,fullS) {
// Replaces oldS with newS in the string fullS
begin = fullS.indexOf(oldS); // begin of replacement
end = begin + oldS.length; //end of replacement
fullS = fullS.substring(0,begin) + newS + fullS.substring(end, fullS.length)
return fullS
}
</script>
</HEAD>

<BODY onLoad=&quot;transform()&quot;>

</BODY>
</HTML>
 
hi
dianal, u can make link from string much easier: string.link(url) fix ur code with this note - it wuld look nicer! regards, vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top