Skip to main content
Known Participant
March 25, 2009
Question

replace issue

  • March 25, 2009
  • 1 reply
  • 371 views
Hi,

I've got a problem, which appears to be quite simple but I'm struggeling to find the solution.

I'm doing a replace on specific strings in my content which is being pulled into my pages.

e.g. <cfset request.vContent = Rereplace(request.vContent, "\bMyWord\b","\bMyNewWord\b", "ALL")>

This words fine... infact it words too well as it also replaces the string "myWord" in anchor text and titles, which is what i need to eliminate.

for example <a href="www.MyWord.com" title="MyWord">MyWord link</a>

becomes: <a href="www.MyNewWord.com" title="MyNewWord">MyNewWord link</a>

when it needs to be: <a href="www.MyWord.com" title="MyWord">MyNewWord link</a>

Thanks in advance for any advice.

Sam.
This topic has been closed for replies.

1 reply

Inspiring
March 25, 2009
Sam_Ham wrote:
> Hi,
>
> I've got a problem, which appears to be quite simple but I'm struggeling to
> find the solution.

For a solution that works 100% I think you'll need to parse the HTML
into a DOM and replace the text only in the text nodes.

A solution that works in 80-90% of the cases:
<cfset request.vContent = Rereplace(request.vContent,,
"([[:space:]<>])MyWord([[:space:]<>])","\1MyNewWord\2", "ALL")>

--
Mack
Sam_HamAuthor
Known Participant
April 1, 2009
Hi Mack,

I tested the the Rereplace code and you're correct, it's not consistant enough for the job in hand.

I'm not quite sure how you can parse the HTML into a DOM and replace the text only in the text nodes. If you could give me some advice on this it would be a great help.