如何在 HTML 特殊实体中使用 Ruby 重音字符进行转换

发布于 2024-08-09 05:59:55 字数 1272 浏览 9 评论 0原文

我怎样才能在 Ruby 上做到这一点?

puts some_method("ò")
# => "ò"

换句话说,将像 ò 这样的重音字符转换为他的 HTML 版本: ò

我尝试了这样的操作:

# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'

coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)

但是我得到了这个(来自: http://htmlentities.rubyforge.org/) :

/Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `unpack': malformed UTF-8 character (expected 2 bytes, given 1 bytes) (ArgumentError)
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `encode_decimal'
 from (eval):2:in `encode_extended'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `gsub!'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities.rb:74:in `encode'
 from unicode_pleasure.rb:8

感谢您的宝贵时间!

  • 列奥纳多

How can I do this on Ruby?

puts some_method("ò")
# => "ò"

In other words convert an accented character like ò to his HTML version: ò

I tried like this:

# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'

coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)

but what I get this (from: http://htmlentities.rubyforge.org/) :

/Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `unpack': malformed UTF-8 character (expected 2 bytes, given 1 bytes) (ArgumentError)
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:85:in `encode_decimal'
 from (eval):2:in `encode_extended'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `gsub!'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities/encoder.rb:18:in `encode'
 from /Library/Ruby/Gems/1.8/gems/htmlentities-4.2.0/lib/htmlentities.rb:74:in `encode'
 from unicode_pleasure.rb:8

Thank you for your time!

  • Leonardo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-08-16 05:59:55

我已明确设置 $KCODE 以使您的示例正常工作。另外,请确保您的源文件实际上编码为 UTF-8

# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'
$KCODE = 'UTF-8'
coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)

I had explicitly set the $KCODE to make your example work. Also, make sure your source file is actually encoded as UTF-8!

# coding: utf-8
require 'rubygems'
require 'htmlentities'
require 'unicode'
$KCODE = 'UTF-8'
coder = HTMLEntities.new
string = "Scròfina"
puts coder.encode(string, :named)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文