# Auto-recognize links?

**URL:** <https://www.ruby-forum.com/t/auto-recognize-links/150475>\
**Category:** Ruby\
**Created:** [October 8, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/auto-recognize-links/150475 "2008-10-08T23:55:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [October 8, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/auto-recognize-links/150475/1 "2008-10-08T23:55:41Z")

</div>

In PHP I’ve got a few functions that will turn data into links based on  
if it recognizes “http://”, “www.” etc etc.

How can I do this with Ruby?

For instance, in a forums setting if a user posted a link to something  
like [http://example.com](http://example.com)

It’d basically turn it into a clickable link:  
\<a  
href="[http://example.com](http://example.com)"\>[http://example.com](http://example.com)\</a\>

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [October 9, 2008, 12:21am UTC](https://www.ruby-forum.com/t/auto-recognize-links/150475/2 "2008-10-09T00:21:48Z")

</div>

I think everything you’d need to do to validate and produce links should  
be in the core URI class: [http://www.ruby-doc.org/core/classes/URI.html](http://www.ruby-doc.org/core/classes/URI.html)

Josh P. wrote:

> In PHP I’ve got a few functions that will turn data into links based on  
> if it recognizes “http://”, “www.” etc etc.
> 
> How can I do this with Ruby?
> 
> For instance, in a forums setting if a user posted a link to something  
> like [http://example.com](http://example.com)
> 
> It’d basically turn it into a clickable link:  
> \<a  
> href="[http://example.com](http://example.com)"\>[http://example.com](http://example.com)\</a\>

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [October 9, 2008, 1:12am UTC](https://www.ruby-forum.com/t/auto-recognize-links/150475/3 "2008-10-09T01:12:43Z")

</div>

On Thu, Oct 9, 2008 at 12:20 AM, Ted H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think everything you’d need to do to validate and produce links should  
> be in the core URI class: [http://www.ruby-doc.org/core/classes/URI.html](http://www.ruby-doc.org/core/classes/URI.html)

Interesting, did not know that. Seems bad though:  
URI.extract(“Visit [Google](http://google.com/.)”) # =\> [“[Google](http://google.com/.)”]

You could borrow code from Rails’ auto\_link() which handles that kind  
of stuff better:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M001737](http://api.rubyonrails.com/classes/ActionView/Helpers/TextHelper.html#M001737)

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [October 10, 2008, 11:03pm UTC](https://www.ruby-forum.com/t/auto-recognize-links/150475/4 "2008-10-10T23:03:41Z")

</div>

On Oct 8, 6:09 pm, Henrik N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > It’d basically turn it into a clickable link:  
> > > \<a  
> > > href="[http://example.com](http://example.com)"\>[http://example.com](http://example.com)
> 
> > –  
> > Posted viahttp://www.ruby-forum.com/.- Hide quoted text -
> 
> - Show quoted text -

Have you looked at hpricot
