简体   繁体   English

Ruby 1.8.7中的String.encode()替代品,用于清理对UTF-8的输入

[英]String.encode() alternative in Ruby 1.8.7 for cleaning input to UTF-8

I just discovered that the String.encode method is only available from Ruby -v 1.9.3 and upwards. 我刚刚发现String.encode方法仅可用于Ruby -v 1.9.3及更高版本。 I'm working in a Rails environment were I cannot change this. 我无法在Rails环境中工作。 I used this method to correct invalid UTF-8 input. 我使用此方法来更正无效的UTF-8输入。

The only good alternative I've found was through the iconv.conv() method, however iconv is deprecated in newer ruby versions and I would like my code to be smelling like flowers even if/when my sysadmin decides to upgrade. 我发现的唯一好的替代方法是通过iconv.conv()方法,但是iconv在较新的Ruby版本中已被弃用,即使我的系统管理员决定升级,我也希望我的代码像花一样闻起来。

For reference, the alternative I found from Here : 作为参考,我从这里找到了替代方案:

ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid_string = ic.iconv(untrusted_string + ' ')[0..-2]

Thanks! 谢谢!

看一下charlock_holmes gem,它满足了1.9版之前的大多数编码要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM