Created
July 12, 2013 10:02
-
-
Save mxlje/5983297 to your computer and use it in GitHub Desktop.
Ruby method for cleaning URL strings for display
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
class String | |
# remove protocol, www, and trailing slash from URL string | |
def to_clean_url | |
u = self | |
if u.start_with?('http://www.') | |
u = u[11..-1] | |
elsif u.start_with?('https://www.') | |
u = u[12..-1] | |
elsif u.start_with?('http://') | |
u = u[7..-1] | |
elsif u.start_with?('https://') | |
u = u[8..-1] | |
end | |
if u.end_with?('/') | |
u = u.chomp('/') | |
end | |
u | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment