CFLib.org – Common Function Library Project

verityClean(input)

Last updated April 24, 2009
Download UDF

author

Simon Potter Simon Potter

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

 
Rated 0 time(s). Average Rating: 0

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
* v6 fix by Mark, when we remove bad chars, replace with space, not nothing
*
* @param input      String to Verity clean. (Required)
* @return Returns a string.
* @author Simon Potter (spotter@redbanner.com)
* @version 6, April 24, 2009
*/

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");
    
    //=-=-=-=-=-=-=-=-
    // Clean up sequences of space characters
    //=-=-=-=-=-=-=-=-
    cleanText = reReplace(cleanText,"[[:space:]]+"," ","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

Tony Blackmon Tony Blackmon added
generatePassword
7 hour(s) ago

Ben Garrett Ben Garrett added
isRFC3339
8 hour(s) ago

Raymond Camden Raymond Camden added
romanToDecimal
4 day(s) ago

Joe Rinehart Joe Rinehart added
directoryCopy
4 day(s) ago

Top Rated

Nick Giovanni                                     UniqueValueList
Rated 5.0, 5 time(s)

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Bobby Hartsfield                                  randStr
Rated 5.0, 3 time(s)

Rob Brooks-Bilson                                 FolderSize
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson