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)
Compare your example to (notice the url highlight):
[webmasterworld.com...]
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...