CFLib.org – Common Function Library Project

RePlaceHolders(thisField, identifier[, query])

Last updated September 20, 2004

author

Joshua Miller

Version: 1 | Requires: CF5 | Library: StrLib

Description:
The RePlaceHolders UDF returns a string of text with variable placeholders turned into the actual value of those variables. This has been useful in CFMAIL functions when sending user information, etc. In the email message you can include: %username% and when the mail is sent it replaces %username% with the actual username. You can specify your own placeholder identifier (%,!,@,$,etc). You can also pass an optional query. If passed, data replacements will be made from the query. (The data will come from the first row.)

Return Values:
Returns a string.

Example:

<cfset name="Mudd">
<cfset myField="My name is %name%">
<cfoutput>
Name: Mudd<br>
String: My name is %name%<br>
Output: #RePlaceHolders(myField,'%')#
</cfoutput>

Parameters:

Name Description Required
thisField The string to check. Yes
identifier The string used as a variable identifier. Yes
query Query to retrive data from. No

Full UDF Source:

/**
 * Replaces variable placeholders with values of said variables.
 * 
 * @param thisField      The string to check. (Required)
 * @param identifier      The string used as a variable identifier. (Required)
 * @param query      Query to retrive data from. (Optional)
 * @return Returns a string. 
 * @author Joshua Miller (josh@joshuasmiller.com) 
 * @version 1, September 20, 2004 
 */
// Replaces variable placeholders with variable values.
function RePlaceHolders(thisField,identifier){
    var start=1;
    var st="";
    var end="";
    var temp="";
    var tempVar="";
        var query = "";
    if (ArrayLen(arguments) EQ 3) {
        query="#arguments[3]#.";
    }
    for(i=1;i lte Len(thisField);i=i+1){
        st=#Find(identifier,thisField,start)#;
        if(st gt 0){
            end=#Find(identifier,thisField,st+1)#+1;
            ct=end-st;
            temp=Mid(thisField,st,ct);
            tempVar=evaluate("###query##ReplaceNoCase(temp,identifier,"","ALL")###");
            thisField=ReplaceNoCase(thisField,temp,tempVar,"ALL");
            if(Len(temp) gt Len(tempVar)){
                end=end-(Len(temp)-Len(tempVar));
            }else{
                end=end+(Len(tempVar)-Len(temp));
            }
            start=end+1;
        }
    }
    return thisField;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson