Tips from the Forums

Displaying code with line breaks

This code is especially useful when data is entered into a text area and inserted into a database. Without any formatting, no line breaks entered by the user would be displayed and the text would run on. The Replace or ReplaceNoCase functions can be used to replace all line breaks with the <br> tag:

<cfoutput>
   #ReplaceNoCase(MyField, "#Chr(13)##Chr(10)#", "<br>", "ALL")#
</cfoutput>


Strip out all non alpha-numeric characters from a file name

Many times users will upload files to your site that contain web-illegal characters in their names, such as spaces. If you wish to retain the original file name but remove those characters, the following code will do the trick:

<cfset NewFileName = REReplaceNoCase(FORM.FileUploadField, "[^[:alnum:]]", "", "ALL")>


Loop over the FORM scope to retrieve all name/value pairs

If you have a dynamic form and don't know all of your field names, the following code will loop over the from scope and display all the name/value paris:

<!--- Loop over a Form Structure and get all the name/value pairs. --->
<cfloop collection="#FORM#" item="VarName">
  <cfoutput>
    #VarName# - #StructFind(FORM, VarName)#<br>
  </cfoutput>
</cfloop>

This code works with all ColdFusion collections, such as Session, Application and URL.


Strip out all HTML from a variable

This one comes straight from the CF Docs and is very helpful. It strips out all HTML tags from a variable:

<cfset StrippedField = REReplaceNoCase(VariableWithHTML, "<[^>]*>", "", "ALL")>
<cfoutput>#StrippedField#</cfoutput>


Retrieve the current URL

This code will retrieve the full, current URL, including port number and URL variables:

<cfset CurrentURL = "http://" & CGI.Server_Name>
<cfif Len(Trim(CGI.Server_Port))>
   <cfset CurrentURL = CurrentURL & ":" & CGI.Server_Port>
</cfif>
<cfset CurrentURL = CurrentURL & CGI.Script_Name>
<cfif Len(Trim(CGI.Query_String))>
   <cfset CurrentURL = CurrentURL & "?" & CGI.Query_String>
</cfif>

Join Our E-Mail Newsletter

For Email Newsletters you can trust


Sister Sites

RiteTech LLC
Modern HOA