开发者

How can I get the equivalent non-utf8 chars from a utf8 string?

开发者 https://www.devze.com 2023-02-06 18:43 出处:网络
ruby 1.8.6, rails 2.3.8 Im doing some evals to write some methods to a class, and its working out nice(first time playing w/ metaprogramming :p), except that some strings that need to go into the met

ruby 1.8.6, rails 2.3.8

Im doing some evals to write some methods to a class, and its working out nice(first time playing w/ metaprogramming :p), except that some strings that need to go into the methods(as code, and not strings) have accented characters and etc...

for example, I need to get "restricao" from the string "restriçã开发者_开发百科o", how can I do that, please?


Check Unidecode gem!

"restrição".to_ascii #=> "restricao"


Use Iconv with TRANSLIT-feature, for example:

require 'iconv'
Iconv.conv("ASCII//TRANSLIT", "UTF-8", "restrição") == "restricao"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号