Skip to content

Instantly share code, notes, and snippets.

@mm580486
Last active January 27, 2021 14:30
Show Gist options
  • Save mm580486/8102d3622ee5ec7e150b35923e9a69f7 to your computer and use it in GitHub Desktop.
Save mm580486/8102d3622ee5ec7e150b35923e9a69f7 to your computer and use it in GitHub Desktop.
remove emoji from string - ruby
# method base
def clean_emoji(str='')
str=str.force_encoding('utf-8').encode
arr_regex=[/[\u{1f600}-\u{1f64f}]/,/[\u{2702}-\u{27b0}]/,/[\u{1f680}-\u{1f6ff}]/,/[\u{24C2}-\u{1F251}]/,/[\u{1f300}-\u{1f5ff}]/]
arr_regex.each do |regex|
str = str.gsub regex, ''
end
return str
end
# sample
clean_emoji "i'm hungry 😭" #=>"i'm hungry"
#module base
module StringHelper
def clean_emoji
str = self.force_encoding('utf-8').encode
arr_regex=[/[\u{1f600}-\u{1f64f}]/,/[\u{2702}-\u{27b0}]/,/[\u{1f680}-\u{1f6ff}]/,/[\u{24C2}-\u{1F251}]/,/[\u{1f300}-\u{1f5ff}]/]
arr_regex.each do |regex|
str = str.gsub regex, ''
end
return str
end
end
class String
include StringHelper
end
# sample
"i'm hungry 😭".clean_emoji #=>"i'm hungry"
@guanting112
Copy link

try this:

gem install remove_emoji

doc:
https://github.com/guanting112/remove_emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment