CFLib.org – Common Function Library Project

verityClean(input)

Last updated December 7, 2007
Download UDF

author

Simon Potter                                      Simon Potter

Version: 5 | Requires: ColdFusion 5 | Library: StrLib

Description:
Verity_clean strips all invalid characters and word combinations from a search strign to prevent verity from crashing.

Return Values:
Returns a string.

Example:

<cfset strs = arrayNew(1)>
<cfset strs[1] = "foo oo ooo">
<cfset strs[2] = "foo@moo">
<cfset strs[3] = "andfoo">
<cfset strs[4] = "and foo">
<cfset strs[5] = "or and foo">
<cfset strs[6] = "\and and foo and">
<cfloop index="x" from=1 to="#arrayLen(strs)#">
    <cfoutput><b>#strs[x]#</b> passed to VerityClean is: <b>#verityClean(strs[x])#</b><P></cfoutput>
</cfloop>

Parameters:

Name Description Required
input String to Verity clean. Yes

Full UDF Source:

<cfscript>
/**
* Creates a verity &quot;safe&quot; search string.
* Version 2 rewritten by Raymond Camden
* Version 3 - made \ into \\ thanks to user comment
* Version 4 - Fixed bugs identified by John Salonich II (21 JAN 2003), Neal Todd (06 FEB 04), Jeremy Halliwell (01 APR 03). Also added fix for curly brackets, comma, funny quote and plus.
* v5 bug fixed by Dominic OConnor
*
* @param input      String to Verity clean. (Required)
* @return Returns a string.
* @author Simon Potter (spotter@redbanner.com)
* @version 5, December 7, 2007
*/

function verityClean(input) {
    //Value to return after cleaning
    var cleanText = trim(input);
    // List of special characters to remove
    var reBadChars = "\\|@|#chr(34)#|'|<|>|\(|\)|!|=|\[|\]|\{|\}|\#chr(44)#|`|\#chr(43)#";
    // List of words to watch for
    var arProblemWords = "and,or,not";    
    var x = 1;
    var y = 1;
    var temp = "";
    
    //=-=-=-=-=-=-=-=-=-=-=-=-=-=
    //Turn list into arrays for the loop
    //=-=-=-=-=-=-=-=-=-=-=-=-=-=
    arProblemWords = ListToArray(arProblemWords);    
    
    //=-=-=-=-=-=-=-=-
    // Strip double spaces
    //=-=-=-=-=-=-=-=-
    cleanText = reReplace(cleanText," {2,}"," ","all");

    //=-=-=-=-=-=-=-=-=-
    // Strip bad characters
    //=-=-=-=-=-=-=-=-=
    cleanText = reReplace(cleanText,reBadChars,"","all");
    
    //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Remove dupes and start/end bad words
    //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    for (x = 1; x lte arraylen(arProblemWords); x = x + 1) {
    
        //remove dupes
        cleanText = trim(reReplace(cleanText,"(#arProblemWords[x]#[[:space:]]+){2,}","","all"));

        //remove arProblemWord[x] + any of the others
        temp = arProblemWords[x] & "[[:space:]]+(";
        for(y = 1; y lte arrayLen(arProblemWords); y=y+1) {
            if(x neq y) {
                temp = temp & arProblemWords[y] & "[[:space:]]+|";
            }
        }
        //remove last |
        temp = left(temp, len(temp)-1);
        //add closing )
        temp = temp & ")";
        cleanText = trim(reReplace(cleanText,temp,"","all"));
        
        //remove from beginning
        if(
            findNoCase(arProblemWords[x],cleanText) is 1 and
            reFind("[[:space:]]+",mid(cleanText,len(arProblemWords[x])+1,1)) and
            mid(cleanText,1,3) NEQ "not"
        ) cleanText = trim(right(cleantext,len(cleantext) - len(arProblemWords[x])));
        
        if(
            findNoCase(arProblemWords[x],cleanText) gt 0 and
            len(cleanText) eq len(arProblemWords[x])
        ) cleanText = "";
        
        //remove from end
if(
findNoCase(arProblemWords[x],cleanText) gt 0 and
findNoCase(arProblemWords[x],cleanText,len(cleanText) - len(arProblemWords[x])) is (len(cleanText) - len(arProblemWords[x]) + 1)
and
reFind("[[:space:]]+",mid(cleanText,len(cleanText) - len(arProblemWords[x]),1))
) cleanText = trim(left(cleanText, len(cleanText) - len(arProblemWords[x])));                    
    }    
    
    // Return the cleaned value
    return cleanText;
}
</cfscript>

Search CFLib.org


Latest Additions

Alan McCollough Alan McCollough added
getSecondsFromTime
8 day(s) ago

Michael Muller Michael Muller added
webmasterEmail
13 day(s) ago

Nick Maloney Nick Maloney added
friendlyURL
15 day(s) ago

Rachel Maxim Rachel Maxim added
delimitInterCap
15 day(s) ago

Raymond Camden Raymond Camden added
stripHTML
15 day(s) ago

Created by Raymond Camden / Design by Justin Johnson