如何摆脱 ruby 中的非 ascii 字符
我有一个 Ruby CGI(不是 Rails),可以从 Web 表单中选取照片和标题。 我的用户非常热衷于使用智能引号和连字,他们从其他来源粘贴。 我的 Web 应用程序不能很好地处理这些非 ASCII 字符,是否有一个快速的 Ruby 字符串操作例程可以摆脱非 ASCII 字符?
I have a Ruby CGI (not rails) that picks photos and captions from a web form. My users are very keen on using smart quotes and ligatures, they are pasting from other sources. My web app does not deal well with these non-ASCII characters, is there a quick Ruby string manipulation routine that can get rid of non-ASCII chars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不,除了基本字符之外,不乏删除所有字符(上面建议的)。 最好的解决方案是正确处理这些名称(因为当今大多数文件系统对于 Unicode 名称没有任何问题)。 如果你的用户粘贴了连字,他们肯定也想把它们找回来。 如果文件系统是您的问题,请将其抽象出来并将文件名设置为某个 md5(这也使您可以轻松地将上传分片到扫描速度非常快的存储桶中,因为它们永远不会有太多条目)。
No there isn't short of removing all characters beside the basic ones (which is recommended above). The best slution would be handling these names properly (since most filesystems today do not have any problems with Unicode names). If your users paste in ligatures they sure as hell will want to get them back too. If filesystem is your problem, abstract it away and set the filename to some md5 (this also allows you to easily shard uploads into buckets which scan very quickly since they never have too many entries).
Quick GS 透露了此讨论,其中建议采用以下方法:
Quick GS revealed this discussion which suggests the following method:
这应该可以解决问题:
This should do the trick:
使用 String#encode
从 Ruby 1.9 开始,在字符串编码之间进行转换的官方方法是使用 字符串#encode。
要简单地删除非 ASCII 字符,您可以这样做:
Use String#encode
The official way to convert between string encodings as of Ruby 1.9 is to use String#encode.
To simply remove non-ASCII characters, you could do this:
Notice that the first 5 characters in the result are "abce1" - the "á" was discarded, one "ë" was discarded, but another "ë" appears to have been converted to "e".
The reason for this is that there are sometimes multiple ways to express the same written character in Unicode. The "á" is a single Unicode codepoint. The first "ë" is, too. When Ruby sees these during this conversion, it discards them.
But the second "ë" is two codepoints: a plain "e", just like you'd find in an ASCII string, followed by a "combining diacritical mark" (this one), which means "put an umlaut on the previous character". In the Unicode string, these are interpreted as a single "grapheme", or visible character. When converting this, Ruby keeps the plain ASCII "e" and discards the combining mark.
If you decide you'd like to provide some specific replacement values, you could do this:
Update
Some have reported issues with the
:universal_newline
option. I have seen this intermittently, but haven't been able to track down the cause.When it happens, I see
Encoding::ConverterNotFoundError: code converter not found (universal_newline)
. However, after some RVM updates, I've just run the script above under the following Ruby versions without problems:Given this, it doesn't appear to be a deprecated feature or even a bug in Ruby. If anyone knows the cause, please comment.
1.9
1.8
1.9
1.8
在 @masakielastic 的帮助下,我使用 #chars 方法出于个人目的解决了这个问题。
诀窍是将每个字符分解为其自己的单独块以便 ruby 可以失败。
当 Ruby 遇到二进制代码等时,需要失败。如果你不允许 Ruby 继续前进并失败,那么当涉及到这些东西时,它会是一条艰难的道路。 因此,我使用 String#chars 方法将给定的字符串分解为字符数组。 然后我将该代码传递到一个清理方法中,该方法允许代码在字符串中包含“微故障”(我的创造词)。
因此,给定一个“脏”字符串,假设您在图片上使用了
File#read
。 (我的情况)With a bit of help from @masakielastic I have solved this problem for my personal purposes using the #chars method.
The trick is to break down each character into its own separate block so that ruby can fail.
Ruby needs to fail when it confronts binary code etc. If you don't allow ruby to go ahead and fail its a tough road when it comes to this stuff. So I use the String#chars method to break the given string into an array of characters. Then I pass that code into a sanitizing method that allows the code to have "microfailures" (my coinage) within the string.
So, given a "dirty" string, lets say you used
File#read
on a picture. (my case)如果您有 activesupport,您可以使用
I18n.transliterate
If you have activesupport you can use
I18n.transliterate
Or if you don't want the question marks...
Note that this doesn't remove invalid byte sequences it just replaces non ascii characters. For my use case this was what I wanted though and was simple.
这是我使用 Iconv 的建议。
Here's my suggestion using Iconv.