Created
May 9, 2010 14:29
-
-
Save mrrooijen/395188 to your computer and use it in GitHub Desktop.
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
# Methods to parse permalinks from strings | |
## Method 1 | |
require 'iconv' | |
class String | |
def to_permalink | |
(Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv self).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase | |
end | |
end | |
## Method 2 | |
require 'unicode' | |
class String | |
def to_permalink | |
str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'') | |
str = str.gsub(/[^-_\s\w]/, ' ').downcase.squeeze(' ').tr(' ','-').gsub(/-+$/,'') | |
end | |
end | |
## Example | |
"this is pretty sweet".to_permalink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment