Skip to content

Instantly share code, notes, and snippets.

@pch pch/gist:771063
Created Jan 8, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.