Skip to content

Instantly share code, notes, and snippets.

@pch
Created January 8, 2011 19:06
Show Gist options
  • Save pch/771063 to your computer and use it in GitHub Desktop.
Save pch/771063 to your computer and use it in GitHub Desktop.
class String
def to_slug
s = self.gsub(/&/, 'and') # replace ampersand chars with 'and' before stripping HTML
s.gsub!(/<.*?>/, '') # strip HTML
s.gsub!(/&/, 'and') # replace ampersand chars with 'and'
s = Iconv.iconv('ascii//ignore//translit', 'utf-8', s).to_s # Borrowed partially from Technoweenie's PermalinkFu
s.gsub!(/\W+/, ' ') # all non-word chars to spaces
s.strip!
s.downcase!
s.gsub!(/[\W^-_]+/, '-') # replace non-word chars with dashes
s.gsub!(/\-{2}/, '-') # remove double dashes
s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment