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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse a clump of text

Status
Not open for further replies.

stormbind

Technical User
Joined
Mar 6, 2003
Messages
1,165
Location
GB
I think this can be done with text ranges but I find myself confused.

For example. Let's just say we wanted to parse this text and replace all the ASCII smilies with images.

Step 1: Find the smiley
// can we use the brower's document search? :-/

Step 2: Select the smiley
// make a text range, right? :)

Step 3: Replace with image
// paste HTML in text range, right? :)

Step 5: Loop
// uh oh. how will it know when to stop looping? maybe it needs seperate loops for each smiley?

If nobody here knows, I'm going to be upset! :(
 
no need to be upset, this is better done in the server side, what language are u using???

Known is handfull, Unknown is worldfull
 
JScript :P

I use PERL/PHP/SSI for serious projects but they aren't available in this scenario :(

The situation is a really weak forum (freebie) that comes with ISP webspace (freebie) and I'm trying to write a JScript template for that forum that will convert it from the abysmal waste of space that it ussually is.

I have done the cosmetics using DOM. To give an idea of what a big task that was, the original forum looked like the stuff below and lacked any HTML: Was just ASCII characters, links and nbsp!

> Test post
> Re: Test Post
> Re: Re: Test Post
> Damn, this suck!
> Re: Damn, this sucks!


I have come up with some plans for adding user athentication, post counts, and avatars (yup... this forum is really that awful that it lacks all of the above!)

And when I'm done, I'm going to donate my script to all those poor saps who use the free web space for their personal web pages - plus I'm getting some satisfaction from this particular project! :)

So there you have it. I would never use client dependent language for commercial applications, but there's no other choice on this occation.

I'm come so far. It actually "looks" like ikonboard or something similar. Not gonna give up now! :P
 
The following works, but it only catches the first smiley. I guess the script needs to loop until it returns false but I cannot get this to work :(

My attempts just loop forever and I need to ALT+CNTRL+DEL to close the browser :P

Code:
<HTML>
<BODY bgcolor=&quot;green&quot;>
Leonardo da Vinci was one of the great masters of the High 
Renaissance, especially in painting, sculpture, architecture, :) 
engineering, and science. :)
</BODY>
</HTML>

<SCRIPT>
var smiley = document.body.createTextRange();
smiley.findText(':)');
smiley.pasteHTML('<img src=&quot;smile.gif&quot;>');
</SCRIPT>
 
o.k, so u want only IE compatibility, then best use DOM, try this:
Code:
<div id=&quot;TxtDiv&quot;>
This is :) number 1.
This is :) number 2.
This is :) number 3.
</div>
<input type=&quot;Button&quot; value=&quot;Replace&quot; onclick=&quot;ReplaceSmiley()&quot;>

<script>
function ReplaceSmiley()
{
 txt=document.getElementById('TxtDiv').innerHTML
 txt=txt.replace(/:\)/g,&quot;<img src='smiley.gif'>&quot;)
 document.getElementById('TextDiv').innerHTML=txt
}
</script>

Known is handfull, Unknown is worldfull
 
Oh cool. Thanks :)

I had just made a script that worked when I saw your post, but mine doesn't change text that has been generated with document.write() so I'm sure your suggestion will come in handy.

Will have to play around with it a little bit to see which is best.

If anyone spots any tweaks, please let me know :)

Code:
<SCRIPT>
var smiley = document.body.createTextRange();
var ascii = new Array(':)',':(',':/');
var image = new Array('smile','sad','worried');
var n=0;

function seek(i){
var c = smiley.findText(ascii[i]);
if (c == true) {
smiley.pasteHTML('<img src=&quot;'+image[i]+'.gif&quot;>');
seek(i);}
}

while (n!=ascii.length) {
seek(n++);}
</SCRIPT>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top