我推荐使用像 Junidecode 这样的 Java 库。
https://github.com/gcardone/junidecode
它将 UTF8 和 UTF16 字符串转换为 ASCII7。例子:
- äéöè = aeoe
- 迈克的咖啡馆 = 迈克的咖啡馆
- ℡ = 电话
- 北亰 = 北京
- まさゆき たけだ 先生 = 武田正之先生
- ⠏⠗⠑⠍⠊⠑⠗ = 总理
- ราชอาณาจักรไทย = raach`aanaacchakraithy
- Ελληνικά = Ellenika
- Москвa = 莫斯科
- Հայաստան = 哈亚斯坦
- ℰℳℒℰ = 例子
我分享了一个完整的基于 ColdFusion 的演示(需要 Junidecode JAR 文件):
https ://dev.to/gamesover/convert-unicode-strings-to-ascii-with-coldfusion-junidecode-lhf
这是代码功能:
<cfscript>
function JUnidecode(inputString){
var JUnidecodeLib = "";
var response = "";
var temp = {};
temp.encoder = createObject("java", "java.nio.charset.Charset").forName("utf-8").newEncoder();
temp.isUTF = temp.encoder.canEncode(arguments.inputString);
if (temp.isUTF){
/* NFKC: UTF Compatibility Decomposition, followed by Canonical Composition */
temp.normalizer = createObject( "java", "java.text.Normalizer" );
temp.normalizerForm = createObject( "java", "java.text.Normalizer$Form" );
arguments.inputString = temp.normalizer.normalize( javaCast( "string", arguments.inputString ), temp.normalizerForm.NFKC );
}
try {
JUnidecodeLib = createObject("java", "net.gcardone.junidecode.Junidecode");
response = JUnidecodeLib.unidecode( javacast("string", arguments.inputString) );
} catch (any e) {
response = "ERROR: JUnidecode is not installed";
}
return trim(Response.replaceAll("\[\?\]", ""));
}
function isDiff(compareArr, val, pos){
return (pos GT arrayLen(comparearr) OR comparearr[pos] neq val);
}
</cfscript>