CFLib.org – Common Function Library Project

scramail(str)

Last updated August 6, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson