網頁

2012年5月17日 星期四

[ROR] rails 轉換 HTMLEntities

今天在轉換資料從 MSSQL (BIG5 編碼) to  MySql (UTF8)

遇到文字內有      ♥     等 HTMLEntities

在 MSSql 需要 encode 才能儲存

但現在 MySql 已經可以一直接存放    = ♥

在Ruby 有好用的gme 可以直接轉換

HTMLEntities

https://github.com/threedaymonk/htmlentities


範例


Decoding

require 'htmlentities'
coder = HTMLEntities.new
string = "élan"
coder.decode(string) # => "élan"

Encoding

This is slightly more complicated, due to the various options. The encode method takes a variable number of parameters, which tell it which instructions to carry out.
require 'htmlentities'
coder = HTMLEntities.new
string = "<élan>"
Escape unsafe codepoints only:
coder.encode(string) # => "&lt;élan&gt;"
Or:
coder.encode(string, :basic) # => "&lt;élan&gt;"

沒有留言:

張貼留言

LinkWithin

Related Posts with Thumbnails