Forum Moderators: coopster & phranque

Message Too Old, No Replies

Higlighting of keywords in a message thread

         

wardbekker

1:24 pm on Jan 29, 2002 (gmt 0)

10+ Year Member



Hi!

I'm currently building a forum. I want to add color highlighting of the keywords like done on this forum : [webmasterworld.com...]

It already worx quite nice, but how can i let my replace statement stay away from HTML text? (otherwise the A anchor to [pizza.com...] wouldn't work because of some span tags in the url eg. www.<span ...>pizza</span>.com)

Brett_Tabke

2:14 pm on Jan 29, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Yes, that is the one very tricky thing about the substitution system. (took me several days to get just right). You have to strip the html and process the words individually afterwords.

Compare your example to (notice the url highlight):
[webmasterworld.com...]

wardbekker

2:24 pm on Jan 29, 2002 (gmt 0)

10+ Year Member



"You have to strip the html and process the words individually afterwords."

Can you clarify that process a bit?

Brett_Tabke

2:32 pm on Jan 29, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I looked at several html stripping routines I've written, and decided that wasn't the route to go. Instead, I went with a hack.

If you have a message: The <b>fox</b> jumped over the lazy <a href="www.dog.com">dog</a>.

How can you cheaply get rid of the html while still knowing it's position? There are complicated regexs that can do it, but for every regex you can show me, I'll show you an unknown exception. So something else is needed in this case.

Say the example sentence is stored in $message:

Here is what I do:
- message is in a scalar $message
- split $message on left bracket into an array @FOO = split(/</,$message)
- that gives you all the html tags starting in the first position one of each element of the array @FOO.
- now go into a new loop to process each line of @FOO.
- split each element on the right brackets into an array. foreach (@foo) @foo2 = split(/>/,$_)
- That puts any html tag into position zero of @foo2 and now all the right hand text is in $foo2...to end of $foo2
- now do your highlighting substitutions on the remaining elements of foo2.
- when complete, connect back up foo2 into one line, and go to the next FOO

The trick is finding all the excpetions...

wardbekker

2:37 pm on Jan 29, 2002 (gmt 0)

10+ Year Member



Smart hack! Thanx