[thelist] CF: using a back reference to address a struct?
Frank Marion
lists at frankmarion.com
Wed Jun 3 03:50:40 CDT 2009
Hey all! Long time no chat!
I'm trying to create a function, much like a user-defined BBCode where
a user can have all instances of [any_given_value] replaced with its
value stored in a struct (and if no matching value exists, that the
[value] is ignored). I'm trying to use a back-reference from
REReplace, but no matter how many weird variations of addressing,
using DE or even Evaluate I use, I can't seem to get it knocked off.
If I use "filter[\1]", my result is: filter[company_name]
If I use "#filter[\1]#", my result is: Missing argument name
If I use "#filter['\1']#", my result is: Element \1 is undefined in a
structure references as part of an expression.
Can anyone help me figure this out? Alternately, if you have a simpler
or more effective way of doing it, I'm all ears.
Look for the REReplace at the variable named text_filtered.
Thanks!
<!--- First, the struct --->
<cfscript>
filter = StructNew();
filter["company_name"] = "ABC Inc";
filter["company_usp"] = "We do stuff";
filter["company_address"] = "123 Main Street";
filter["company_phone"] = "919 555 1212";
filter["company_president"] = "Mr. Wilbur Burrows";
</cfscript>
<cfsavecontent variable="text">
This is the story of a company called <strong>[company_name]</strong>.
They say that their motto is <strong>[company_usp]</strong>. One day,
the president, a certain <strong>[company_president]</strong> used a
<strong>[fake_name]</strong> and called the company at
<strong>[company_phone]</strong> to find out if the address was really
<strong>[company_address]</strong>. It was.
</cfsavecontent>
<cfset text_filtered = REReplaceNoCase(text,"\[([a-zA-Z0-9-_]+)
\]","filter[\1]","all")>
<div style="border: 1px solid black;padding:10px;">
<p><strong>Before</strong><br>
<cfoutput>#text#</cfoutput></p>
</div>
<div style="border: 1px solid red;padding:10px;">
<p><strong>After</strong><br>
<cfoutput>#text_filtered#</cfoutput></p>
</div>
--
Frank Marion
lists [_at_] frankmarion.com
More information about the thelist
mailing list