CFLib.org – Common Function Library Project

scramail(str)

Last updated August 06, 2013

author

Deva Nando

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Scramail takes a string as an argument, finds any emails within the string, changes the characters in the email to their ascii equivelents, and then returns the transformed string. The purpose is to hide any email addresses in a block of text from spam bots. Scramail is a synthesis of Raymond Camden's getEmails and Seth Duffey's EmailAntiSpam UDFs.

Return Values:
Returns a string.

Example:

<cfset strText = "Hi there, you can email me at myemail@somedomain.com">
<cfset strText =  scramail(strText)>
<cfoutput>#strText#</cfoutput>

Parameters:

Name Description Required
str String to parse. Yes

Full UDF Source:

/**
 * Scramail takes a string as an argument and changes the characters in the email to their ascii equivelents to hide the email address from spam bots.
 * v1.0 by Deva Nando
 * v1.1 by Steve Sobol (fixed bug when processing multiple email addresses)
 * 
 * @param str      String to parse. (Required)
 * @return Returns a string. 
 * @author Deva Nando (d.nando@gmail.com) 
 * @version 1.1, August 6, 2013 
 */
function scramailNew(str) {
    var emailregex = "(['_a-z0-9-]+(\.['_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name)))";
    var i = 1;
    var email = "";
    var ascMail = "";
    var marker = 1;
    var matches = "";

    matches = reFindNoCase(emailregex, str, marker, true);
    while (matches.len[1] gt 0) {
        email = mid(str, matches.pos[1], matches.len[1]);
        for (i=1; i LTE len(email); i=i+1) {
            ascMail = ascMail & "&##" & asc(mid(email,i,1)) & ";";
        }
        str = replace(str,email,ascmail,"all");
        marker = matches.pos[1] + matches.len[1];
        matches = reFindNoCase(emailregex, str, marker, true);
        ascmail = "";
    }
    return str;
}

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