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("");
}

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