Page 1 of 1

Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 5:31 am
by abeall
I'm trying to highlight certain keywords in an HTML string. The general approach is to replace [substr] with <span class="hilite">[substr]</span>. However I'm having a hard to figuring out how to do that only in text nodes of the HTML, not inside tags. For instance if I want to hilite the word "img" I do not want it to replace all the image tags.

This is not a valid XML document, either.

My only guess is that RegEx will come to rescue, maybe a pattern where the substr must have a ">" to its left before a "<"?

Re: Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 5:36 am
by SvanteH
Is it possible for you to check for e.g <img> or [img] instead of just the pure text word img?

Re: Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 5:37 am
by abeall
You're misunderstanding me, or I'm misunderstanding you :)
I'm having a hard to figuring out how to do that only in text nodes of the HTML, not inside tags. For instance if I want to hilite the word "img" I do not want it to replace all the image tags.

Re: Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 5:42 am
by SvanteH
I'm most likely missunderstanding you.

Do you have a string containing the html of a webpage and there is texts e.g [img] that you want to encase with a <span>? If so <img src=""> won't cause any trouble

Re: Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 5:52 am
by abeall
Here's an example:

Code: Select all

You're misunderstanding me, or I'm misunderstanding you <img src='misunderstanding.gif'>
@hilite = "misunderstanding"

I want:

Code: Select all

You're <span class="hilite">misunderstanding</span> me, or I'm <span class="hilite">misunderstanding</span> you <img src="misunderstanding.gif">
I do not want (and am currently getting with a simple str_replace):

Code: Select all

You're <span class="hilite">misunderstanding</span> me, or I'm <span class="hilite">misunderstanding</span> you <img src="<span class="hilite">misunderstanding</span>.gif">
Which of course totally borks the <img> tag. I want to exclude my substr replace from happening inside < and >

Re: Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 6:37 am
by SvanteH
If you can assume that it's full words you're going to replace just replace

" $keyword "
" $keyword."
" $keyword,"

Re: Find and replace substr outside HTML tags only

Posted: Wed Jul 08, 2009 7:21 am
by abeall
Yeah, that won't work...