2
我正在處理我的第一個涉及API的項目。將"e;和'轉換爲適當的字符
我正在使用的API使我像字符串:
"In our nation’s capital, killings have risen by 50 percent."
有沒有一種方法,我可以簡單地將代碼轉換成字符?或者我是否需要一遍又一遍地撥打gsub
?
我正在處理我的第一個涉及API的項目。將"e;和'轉換爲適當的字符
我正在使用的API使我像字符串:
"In our nation’s capital, killings have risen by 50 percent."
有沒有一種方法,我可以簡單地將代碼轉換成字符?或者我是否需要一遍又一遍地撥打gsub
?
: [email protected]; sudo gem install htmlentities
Successfully installed htmlentities-4.2.4
: [email protected]; irb
irb(main):001:0> require 'htmlentities'
=> []irb(main):002:0> HTMLEntities.new.decode "¡I'm highly annoyed with character references!"
=> "¡I'm highly annoyed with character references!"
How do I encode/decode HTML entities in Ruby?
我希望這會幫助你。
在Ruby on Rails的,你可以試試下面這行顯示以及HTML
<%=""In our nation’s capital, killings have risen by 50 percent."".html_safe%>
它會輸出如下:「在我們國家的首都,屠殺了50%的增長。」
https://rubygems.org/gems/htmlentities –