Rip's Domain

Friday Puzzle: Actually I need some help :)

Posted in ColdFusion by rip747 on October 12, 2007

UPDATE: So I feel like a complete dumb ass since thanks to Angelos🙂 I swear I was doing what they suggested and it wasn’t working for me. Last time I code and post after a 16 programming marathon.

Does anyone out there know of a way to replace part of a string with a coldfusion variable and evaluate it? Take for instance the following code:

<cfset mystr = “tony”>
<cfset str = “this is a %%test%% to see if this %%works%% or not”>
<cfset str = rereplacenocase(str, “%%([^%%]*)%%”, “##mystr##”, “all”)>
<cfoutput>#str#</cfoutput>

What I’m trying to do is to replace all occurrences of “%%test%%” with “#mystr#” and when I output the str variable, it will replace “#mystr#” with “tony”. Right now when I out then text I get the following:

this is a #mystr# to see if this #mystr# or not

I would like to get it to output like so:

this is a tony to see if this tony or not

Anyone everyone do this?

3 Responses

Subscribe to comments with RSS.

  1. Angelos said, on October 12, 2007 at 3:45 am

    Just use

    #evaluate(de(str))#
    🙂

  2. Peter Bell said, on October 12, 2007 at 6:45 am

    If you want it to evaluate in real time, you can just dump the quotes and hashes around mystr in the replace:

    This assumes that mystr is a valid variable at the time you do the replacement (which is true in your example code). If that isn’t true then it gets a little more difficult.

  3. Peter Bell said, on October 12, 2007 at 6:46 am

    Oops – it ate my code. Lets see if escaping the brackets will help:

    <cfset str = rereplacenocase(str, “%%([^%%]*)%%”, mystr, “all”)>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: