Ruby Linkify for Urls in Strings

Ruby linkify for urls in strings

I might be missing some context, but why re-invent the wheel? Have you tried auto_link in actionpack?

$ gem install actionpack

$ irb -f --prompt simple
>> require 'action_view'
>> include ActionView::Helpers

>> auto_link("abc http://google.com xyz")
=> "abc <a href=\"http://google.com\">http://google.com</a> xyz"
>> auto_link("abc <a href='http://google.com'>google</a> xyz")
=> "abc <a href='http://google.com'>google</a> xyz"

C# code to linkify urls in a string

It's a pretty simple task you can acheive it with Regex and a ready-to-go regular expression from:

  • http://regexlib.com/

Something like:

var html = Regex.Replace(html, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+" +
"\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?" +
"([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$",
"<a href=\"$1\">$1</a>");

You may also be interested not only in creating links but in shortening URLs. Here is a good article on this subject:

  • Resolve and shorten URLs in C#

See also:

  • Regular Expression Workbench at MSDN
  • Converting a URL into a Link in C# Using Regular Expressions
  • Regex to find URL within text and make them as link
  • Regex.Replace Method at MSDN
  • The Problem With URLs by Jeff Atwood
  • Parsing URLs with Regular Expressions and the Regex Object
  • Format URLs in string to HTML Links in C#
  • Automatically hyperlink URL and Email in ASP.NET Pages with C#

Ruby on Rails - Converting Twitter @mentions, #hashtags and URLs within a string

The twitter-text gem has pretty much all the work covered for you. Install it manually (gem install twitter-text, use sudo if needed) or add it to your Gemfile (gem 'twitter-text') if you are using bundler and do bundle install.

Then include the Twitter auto-link library (require 'twitter-text' and include Twitter::Autolink) at the top of your class and call the method auto_link(inputString) with the input string as the parameter and it will give you the auto linked version

Full code:

require 'twitter-text'
include Twitter::Autolink

myString = "I like using @twitter, because I learn so many new things! [line break]
Read my blog: http://www.myblog.com #procrastination"

linkedString = auto_link(myString)

If you output the contents of linkedString, you get the following output:

I like using @<a class="tweet-url username" href="https://twitter.com/twitter" rel="nofollow">twitter</a>, because I learn so many new things! [line break] 
Read my blog: <a href="http://www.myblog.com" rel="nofollow">http://www.myblog.com</a> <a class="tweet-url hashtag" href="https://twitter.com/#!/search?q=%23procrastination" rel="nofollow" title="#procrastination">#procrastination</a>

Rails 3 Make Text into a Clickable Link

So say your object is in @post

In the display, do <%= link_to @post.url, @post.url %>

That should "linkify" your URL so that users can click it.

Regex to make links clickable (in only 'a href' and not 'img src')

JavaScript lacks support of negative lookbehinds in regular expressions. Here's the simple workaround:

var content = '<a href="http://google.com">Google.com</a> and http://google.com';

var re = /((?:href|src)=")?(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;

content = content.replace(re, function (match, attr) {
if (typeof attr != 'undefined') {
return match;
}
return '<a target="_blank" href="' + match + '">' + match +'</a>';
});

But you should avoid parsing HTML with RegExp. Here's why.

Replacing the 'auto_link' method in Ruby on Rails 3.1

Rinku is a drop-in replacement for Rails 3.1 auto_link.

Auto-linking functionality has been removed from Rails 3.1, and is instead offered as a standalone gem, rails_autolink. You can choose to use Rinku instead.

require 'rails_rinku'

The rails_rinku package monkeypatches Rails with an auto_link method that mimics 100% the original one, parameter per parameter. It's just faster.



Related Topics



Leave a reply



Submit