Created
June 8, 2009 21:58
-
-
Save davesouth/126091 to your computer and use it in GitHub Desktop.
Transliterate to lowercase, letters and numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on permalink_fu by Rick Olsen | |
def transliterate(str) | |
# Escape str by transliterating to UTF-8 with Iconv | |
s = Iconv.iconv('ascii//ignore//translit', 'utf-8', str).to_s | |
# Downcase string | |
s.downcase! | |
# Remove apostrophes so isn't changes to isnt | |
s.gsub!(/\'/, '') | |
# Replace any non-letter or non-number character with a space | |
s.gsub!(/[^A-Za-z0-9]+/, ' ') | |
# Remove spaces from beginning and end of string | |
s.strip! | |
# Replace groups of spaces with single hyphen | |
s.gsub!(/\ +/, '-') | |
return s | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment