Forum Moderators: coopster

Message Too Old, No Replies

preg replace a word. but only if it's inside <p>

         

londrum

1:56 pm on Sep 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



got a bit of a head scratcher.

i've got this piece of code which changes any occurance of the word 'blah' inside a tag, with the word 'bleugh' (...except when it's in an <a> tag)

$html = preg_replace ("//(?!(?:[^<]+>|[^>]+<\/a>))(blah)/is", "bleugh", $html);


this is pretty close to what i need. but what i really want to do is change 'blah' to 'bleugh' ONLY when it's inside a <p> tag.

i can do this pretty easy when the example is just
<p>blah</p>


but i can't figure out how to do it when there are multiple cases of 'blah' inside a variety of different tags
<a href="blah.html">blah</a>

<p>the quick brown fox blah jumped over


in this second example, the only one i want to change is in the <p> tag on the second line... brown fox blah jumped...

coopster

8:17 pm on Dec 15, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Did you resolve this, londrum? It is likely going to involve, at the very least, lookahead and/or lookbehind assertions, possibly negative assertions.