Skip to content

Instantly share code, notes, and snippets.

@hoanghiep90
Last active August 22, 2017 08:54
Show Gist options
  • Save hoanghiep90/85146b008129af69a13ced02fbae643a to your computer and use it in GitHub Desktop.
Save hoanghiep90/85146b008129af69a13ced02fbae643a to your computer and use it in GitHub Desktop.
# encoding: utf-8
String.class_eval do
def strip_accents
accents = {
['à','À','ả','Ả','ã','Ã','á','Á','ạ','Ạ','ă','Ă','ằ','Ằ','ẳ','Ẳ','ẵ','Ẵ','ắ','Ắ','ặ','Ặ','â','Â','ầ','Ầ','ẩ','Ẩ','ẫ','Ẫ','ấ','Ấ','ậ','Ậ'] => 'a',
['đ','Đ'] => "d",
['è','È','ẻ','Ẻ','ẽ','Ẽ','é','É','ẹ','Ẹ','ê','Ê','ề','Ề','ể','Ể','ễ','Ễ','ế','Ế','ệ','Ệ'] => "e",
['ì','Ì','ỉ','Ỉ','ĩ','Ĩ','í','Í','ị','Ị'] => 'i',
['ò','Ò','ỏ','Ỏ','õ','Õ','ó','Ó','ọ','Ọ','ô','Ô','ồ','Ồ','ổ','Ổ','ỗ','Ỗ','ố','Ố','ộ','Ộ','ơ','Ơ','ờ','Ờ','ở','Ở','ỡ','Ỡ','ớ','Ớ','ợ','Ợ'] => "o",
['ù','Ù','ủ','Ủ','ũ','Ũ','ú','Ú','ụ','Ụ','ư','Ư','ừ','Ừ','ử','Ử','ữ','Ữ','ứ','Ứ','ự','Ự'] => "u",
['ý','Ý', 'ỷ','Ỷ', 'ỳ','Ỳ', 'ỹ', 'Ỹ', 'ỵ', 'Ỵ'] => "y"
}
str = self
accents.each do |ac,rep|
ac.each do |s|
str = str.gsub(s, rep)
end
end
str.downcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment