将Javascript UTF-8转换为ASCII(例如PHP中的Iconv('UTF-8','ASCII//TRANSLIT',$ string)) [英] Convert Javascript UTF-8 to ASCII (like Iconv('UTF-8', 'ASCII//TRANSLIT', $string) in PHP)

查看:454
本文介绍了将Javascript UTF-8转换为ASCII(例如PHP中的Iconv('UTF-8','ASCII//TRANSLIT',$ string))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Javascript将UTF-8中的字符转换"为最接近的ASCII等效字符,就像Iconv在PHP中一样.

I'm wondering how it's possible to 'translate' characters in UTF-8 to the closest ASCII equivalent using Javascript, just like Iconv doest in PHP.

示例:

ü变成u
ó变成o

ü becomes u
ó becomes o

我宁愿不使用替换,因为a)它需要一组完整的字符,这是很多工作,并且b)我将很难获得一组完整的字符,并且我会永远不确定我是否会错过一两个.

I'd rather not use a replace, because a) it requires a complete set of characters, which is a lot of work and b) i'd would be hard to get a complete set of characters, and i'll never be certain if i'm missing one or two.

推荐答案

正如@Pointy所说,您唯一的选择是根据字典来映射/替换字符.

As @Pointy said, your only option is to map/replace characters according to a dictionary.

您会发现这真的很有用: https://github.com/backbone-paginator/backbone.paginator/blob/a579796a30e583c4dfa09e0a86e4abd21e0b5b56/plugins/diacritic.js

You'll find this really useful: https://github.com/backbone-paginator/backbone.paginator/blob/a579796a30e583c4dfa09e0a86e4abd21e0b5b56/plugins/diacritic.js

这篇关于将Javascript UTF-8转换为ASCII(例如PHP中的Iconv('UTF-8','ASCII//TRANSLIT',$ string))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆