user125264
user125264

Reputation: 1827

convert string to variable coldfusion

I'm a bit stumped on this one..

I currently have a string.

Please enter your variable.firstname here

What i would like to do is find the variable.firstname in the string and convert it to be used as #variable.firstname#

Im using CF8, and ive looked at using findNoCase() but the variable.firstname portion can appear anywhere. I am also trying to use this in a Coldfusion Custom Tag as its to simply display the firstname of the user that could be dynamically populated.

I cant use any other functionality to change it IE = variable['firstname] because the variable could be the result of a dynamic variable i pass in and the query for the content will reside within the custom tag.

Upvotes: 0

Views: 888

Answers (3)

imthepitts
imthepitts

Reputation: 1647

If you're trying to replace variable.firstname with #variables.firstname# and then also get the value of that variable, you'll need to do the replace <cfset yourNewString = replace(yourOldString,'variable.firstname', '##variables.firstname##', 'all')> and then wrap the resulting string in an evaluate() function (and an inner de() to prevent CF from evaluating everything): <cfset evaluatedString = evaluate(de(yourNewstring))>.

If there are more variables besides variable.firstname that need this kind of translation, you'll need to get into regex with reReplace() to catch them all in one statement. My regex is rusty, so you'll need to Google that bit on your own. ;o)

Upvotes: -1

genericHCU
genericHCU

Reputation: 4446

I cant use any other functionality to change it IE = variable['firstname] because the variable could be the result of a dynamic variable i pass in and the query for the content will reside within the custom tag.

I'm not sure I understand exactly what you're saying here but if you're saying that variables.firstname is coming from another variable and the .firstname is the dynamic part you could still use array notation.

<cfset myName = "Travis">
<cfset yourName = "user125264">
<cfset myCustomVariable = "myName">
<cfoutput>Hi, My name is #variables[myCustomVariable]#. </cfoutput>
<cfset myCustomVariable = "yourName">
<cfoutput>Your name is #variables[MyCustomVariable]#.</cfoutput>

Output: Hi, My name is Travis. Your name is user125264.

If that isn't what you meant, I apologize.

Upvotes: 0

Dan Bracuk
Dan Bracuk

Reputation: 20804

 <cfset yourNewString = replace(yourOldString,'variable.firstname', 
'##variable.firstname##', 'all')>

Note the double pound signs.

Upvotes: 4

Related Questions