Randomize a Replace Problem…

I have a few paragraphs of text.  I’m trying to replace the text “keyword” with a few things.  Ideally I’m doing…

<cfset text = “This is a sentence about keyword and keyword is really a keyword of this sentence about keyword.”>
<cfset newtext = ReReplaceNoCase(text, "keyword", " <strong>keyword</strong> ", "all")>

However, I would like to randomize this.  I don’t want every instance of “keyword” replaced with <strong>keyword</strong>.  I want to alternate the replacements between…

<a href=”…”>keyword</a>
<font color=”red”>keyword</font>

How might I go about doing this??

Thanks for the pointers!
Who is Participating?
That function wont work, since as soon as it returns a string, that'll be the string that Replace uses for each keyword, so it'll be random as to wether or not the keyword will be replaced, but not specific instances of the keyword.  Here is a function I put together for you to try:

<cffunction name="randomReplacement" output="true" returntype="string">
 <cfargument name="inputString" type="string" required="yes">
 <cfargument name="oldKeyword" type="string" required="yes">
 <cfargument name="newKeyword" type="string" required="yes">

 <cfset placeHolderNew = '**_NEW_**'>
 <cfset placeHolderOld = '**_OLD_**'>
 <cfset listDelims = ' ,.!?'>
 <cfset newString = inputString>

 <cfset findOldKeyword = 1>
 <cfloop condition="findOldKeyword gt 0">
  <cfset findOldKeyword = ListFindNoCase(newString, oldKeyword, listDelims)>
  <cfif findOldKeyword gt 0>
   <cfif RandRange(1,2) is 1>
    <cfset newString = ListSetAt(newString, findOldKeyword, placeHolderNew, listDelims)>  
    <cfset newString = ListSetAt(newString, findOldKeyword, placeHolderOld, listDelims)>  
 <cfset newString = ReplaceNoCase(newString, placeHolderNew, newKeyword, 'ALL')>
 <cfset newString = ReplaceNoCase(newString, placeHolderOld, oldKeyword, 'ALL')>
 <cfreturn newString>

I tested it like so:

<cfloop index="i" from="1" to="30">
 <cfinvoke method="randomReplacement" inputString="My dog is brown, is your dog, sir?" oldKeyword="dog" newkeyword="cat" returnvariable="testing">

And do get 30 seemlingly random results :)

If you can think of other things that could be attached to a keyword besides the listDelims I included, just tag them on to that list.
rebiesAuthor Commented:
One thought I just had, can I put a function inside of the ReReplaceNoCase??

<cffunction name="RandReplace">
      <cfargument name="tobereplaced" type="string" required="Yes">
      <cfif **randomnumber** EQ 1>
            <cfreturn "<strong>#tobereplaced#</strong>">
            <cfreturn "<u>#tobereplaced#</u>">

<cfset newtext = ReReplaceNoCase(text, "keyword", RandReplace(“keyword”), "all")>
rebiesAuthor Commented:
Sorry usachrisk1983, I forgot I left this question open.  Yes, your solution did help me out.  Thanks!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.