1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Replace exact string

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

  1. #1
    I'm outputting text content to my page by querying our database using a simple method, which works fine.
    
    <cfoutput query="getPageContent" startrow="#vPageContent#" maxrows="1">#vContent#</cfoutput>
    
    Code (markup):
    I need to replace certain words to make them bold, for example...
    
    <cfset request.vContent = replace(request.vContent, "phrase", "<B>phrase</B>, "ALL")>
    
    Code (markup):
    This works, but there is a slight issue, because it will also replace occurances of the word 'phrase' even if it is within another word...

    for example, cophrasetastic

    So i need to replace only the exact phrase when used on it's own.

    My problem appears quite simple, but I have not found a way to do this...?
     
    Sushi_Master, Mar 4, 2009 IP
  2. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change:
    replace(request.vContent, "phrase", "<B>phrase</B>, "ALL")

    To:
    replace(request.vContent, " phrase ", " <B>phrase</B> ", "ALL")

    If you want to catch commas and punctuation as well, then you can use a regex to match only the characters you want.
     
    robhustle, Mar 5, 2009 IP