replace issue

Discussion in 'Programming' started by Sushi_Master, Mar 25, 2009.

  1. #1
    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")>
    Code (markup):
    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>
    Code (markup):
    becomes:
    <a href="www.MyNewWord.com" title="MyNewWord">MyNewWord link</a>
    Code (markup):
    when it needs to be:
    <a href="www.MyWord.com" title="MyWord">MyNewWord link</a>
    Code (markup):
    Thanks in advance for any advice.

    Sam.
     
    Sushi_Master, Mar 25, 2009 IP
  2. Paul_K

    Paul_K Greenhorn

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #2
    Replace >MyWord not just MyWord


    <cfset test = '<a href="www.MyWord.com" title="MyWord">MyWord link</a>' />

    <cfset vContent = Rereplace(test, ">MyWord",">MyNewWord", "ALL")>

    <cfdump var="#vContent#" />
     
    Paul_K, Apr 1, 2009 IP
  3. OneWebAve

    OneWebAve Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or use a regular expression to only replace words that are not within < and > ;)
     
    OneWebAve, Apr 12, 2009 IP