7

Using Ruby/Rails does anyone know how to take a large string that may contain some HTML elements and make them into links?

This is an example:

"Check out my video on you tube http://youtu.be/OkCcD6cOKgs"

I am looking for something that will turn the HTML into a valid click-able link <a href ... but also leave the other text as is, just like this question did.

2
  • What do you mean by "HTML elements"? Are they URLs, as in the sample string, or tags and markup? If they are only URLs, are they always at the end of a string, or can the URL be followed by punctuation or more words? You need to define these things in order to get good answers that solve your needs. It's easy to find a solution that works for the sentence above. Let's find one that finds a solution for all the strings you'll have. Commented Apr 15, 2013 at 14:43
  • This question has been answered here: stackoverflow.com/questions/3095230/… Commented Nov 5, 2015 at 0:04

5 Answers 5

12

I know it is too late, But if someone still have this question then he can do this way.

text = "Check out my video on you tube http://youtu.be/OkCcD6cOKgs"
html_text = text.gsub(URI::DEFAULT_PARSER.make_regexp, '<a href="\0">\0</a>').html_safe

This is best way I found from here Ruby: make plain text links clickable till now.

URI.regexp is deprecated, that's why I updated to URI::DEFAULT_PARSER.make_regexp

Sign up to request clarification or add additional context in comments.

2 Comments

This seem to work but I'm experiencing a bug. I have a text like this "View full list on this link: https://google.com/" it gives me "View full list on this <a href=\"link:\">link:</a> <a href=\"https://google.com/\">https://google.com/</a>" where any word with colon on the end seems to match the regex.
This works! Note: URI.regexp is deprecated, use URI::DEFAULT_PARSER.make_regexp instead.
7

Rails used to have a built in "auto_link" function, which now can be brought in as an 'autolink' gem. This does other string sanitization too.

Alternatively the 'anchored' gem is more focused on this one task, or the 'rinku' gem promises to be faster/better.

Without gems, here's the ruby code I have used:

  url_regexp = %r{
    (?:(?:https?|ftp|file):\/\/|www\.|ftp\.)
    (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
         [-A-Z0-9+&@#\/%=~_|$?!:,.])*
    (?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|
          [A-Z0-9+&@#\/%=~_|$])
  }ix

  text = text.gsub(url_regexp, '<a href="\0">\0</a>').html_safe

I recommend you carefully test some edge cases with whatever solution you go for. Looking around for answers to this you'll find people recommending the core library URI.regexp or URI::DEFAULT_PARSER, but this fails very simple tests. Try including a word followed by a colon ':' char in your text. You don't want that to become a link! (I don't really know why this built-in solution is so poor. My guess that is it's more intended for validating rather than finding URL-like substrings)

So my solution above is a regexp based on this answer and converted to be more rubyish. But when you start looking, there are many different regexps for URLs out there. You can play the game of finding the awkward edge case that any given regexp fails on. Generally longer regexps catch more edge cases. But there's also a challenge to convert it to work in ruby. This website presents many language solutions including a (quite long) ruby example: http://urlregex.com

Comments

0

Split by last delimiter: How to split a string into only two parts, by the last occurrence of the split char?

text, uri = "Check out my video on you tube http://youtu.be/OkCcD6cOKgs".split(/ (?=\S+$)/)

<%= link_to text, uri %>

Comments

-2

You can mark the string as html_safe. I believe all you need to do is string.html_safe.

This is if you are asking how to display the HTML elements in the string correctly when they already exists in the string.

If you want to change a URL to an HTML link, you can always use a helper method to check if http(s) exists in the string, and add the HTML element accordingly.

1 Comment

That allows html tags to work, but I am looking for a way to turn the string with a url i.e. "http..." into a clickable link automatically so the user does not have to type <a href="foo">bar</a>
-4

HTML may be easier. In your view:

<h2>    <!--Or whatever tag you're using-->
  Check out my video on  
  <a href="http://http://youtu.be/OkCcD6cOKgs/">YouTube</a>
</h2>

2 Comments

How is this easier? How does the OP get from a string containing a URL to HTML? That's what the question is about.
The way the question is worded, I wasn't exactly clear on what it's about (i.e. if this is an element added to his site in a comments section, or if it's something he's adding to his views, or if this is really just a straight Ruby question even though he mentions Rails).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.