CFLib.org – Common Function Library Project

deAccent(str)

Last updated December 20, 2012

author

Rachel Lehman

Version: 1 | Requires: CF6 | Library: StrLib

Description:
Replaces accented characters in a string with their closest non-accented equivalent, such as French, Spanish and German vowels. Useful when creating filenames, etc. from people's names.

Return Values:
Returns a string.

Example:

<cfoutput>#deAccent('PĂ©rez')#</cfoutput>

Parameters:

Name Description Required
str String within which to replace accented characters Yes

Full UDF Source:

/**
 * Replaces accented characters with their non accented closest equivalents.
 * version 1.0 by Rachel Lehman
 * version 1.1 by Pat Branley (improved portability, fixed bug with &quot;x&quot; remapping
 * version 1.2 by Nathan Dintenfass (used more thorough Java-based approach)
 * 
 * @param str 	 String within which to replace accented characters (Required)
 * @return Returns a string. 
 * @author Rachel Lehman (raelehman@gmail.com) 
 * @version 1.2, December 20, 2012 
 */
function deAccent(str){
	//based on the approach found here: http://stackoverflow.com/a/1215117/894061
	var Normalizer = createObject("java","java.text.Normalizer");
	var NormalizerForm = createObject("java","java.text.Normalizer$Form");
	var normalizedString = Normalizer.normalize(str, createObject("java","java.text.Normalizer$Form").NFD);
	var pattern = createObject("java","java.util.regex.Pattern").compile("\p{InCombiningDiacriticalMarks}+");
	return pattern.matcher(normalizedString).replaceAll("");
}
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