Solved

Randomize a Replace Problem…

Posted on 2006-07-16
3
163 Views
Last Modified: 2013-12-24
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")>
<cfoutput>#newtext#</cfoutput>

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…

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

How might I go about doing this??

Thanks for the pointers!
0
Comment
Question by:rebies
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Author Comment

by:rebies
ID: 17119621
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>">
      <cfelse>
            <cfreturn "<u>#tobereplaced#</u>">
      </cfif>
</cffunction>

<cfset newtext = ReReplaceNoCase(text, "keyword", RandReplace(“keyword”), "all")>
---
0
 
LVL 13

Accepted Solution

by:
usachrisk1983 earned 500 total points
ID: 17122316
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)>  
   <cfelse>
    <cfset newString = ListSetAt(newString, findOldKeyword, placeHolderOld, listDelims)>  
   </cfif>
  </cfif>
 </cfloop>  
 
 <cfset newString = ReplaceNoCase(newString, placeHolderNew, newKeyword, 'ALL')>
 <cfset newString = ReplaceNoCase(newString, placeHolderOld, oldKeyword, 'ALL')>
 
 <cfreturn newString>
   
</cffunction>

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">
 <cfoutput>#testing#</cfoutput><br>
</cfloop>

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.
0
 
LVL 1

Author Comment

by:rebies
ID: 17218974
Sorry usachrisk1983, I forgot I left this question open.  Yes, your solution did help me out.  Thanks!

Andrew
0

Featured Post

Save the day with this special offer from ATEN!

Save 30% on the CV211 using promo code EXPERTS30 now through April 30th. The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question