How to Automatically Link URLs in Rails

4 Jun 2007 in Programming, Ruby on Rails by Garrick

Let’s say you have a Rails app and you’d like any URLs in any plain text string to be automatically hyperlinked. Well, then you’ll need this:
.gsub(/((http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?)/, ‘<a href=\’\1\’>\1</a>’)

For example:
I blog at http://garrickvanburen.com

Through:
"I blog at http://garrickvanburen.com".description.gsub(/((http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?)/, ‘<a href=\’\1\’>\1</a>’)

Results in:
I blog at http://garrickvanburen.com


Comment | Trackback URL Short Link:

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Related Entries




Creative Commons License
About Sitemap XHTML Sitemap XML
Wordpress theme is a heavily hacked version of "Modicus Remix" by Art Culture. Original by Upstart Blogger