Gra*_*eme 2 twitter ruby-on-rails
假设我有一个字符串,其中包含从Twitter抓取的文本,如下所示:
myString = "I like using @twitter, because I learn so many new things! [line break]
Read my blog: http://www.myblog.com #procrastination"
Run Code Online (Sandbox Code Playgroud)
然后在视图中显示推文.但是,在此之前,我想转换字符串,以便在我看来:
我敢肯定那里必须有一颗宝石可以让我这样做,但我找不到一颗.我遇到了twitter-text-rb,但我不太清楚如何将它应用到上面.我使用正则表达式和其他一些方法在PHP中完成它,但它有点乱!
在此先感谢任何解决方案!
Suh*_*tel 10
在Twitter的文字宝石拥有几乎所有涉及你的工作.手动安装(gem install twitter-text如果需要,使用sudo)或将其添加到Gemfile(gem 'twitter-text')中(如果您使用的是Bundler )bundle install.
然后在类的顶部包含Twitter自动链接库(require 'twitter-text'和include Twitter::Autolink),并auto_link(inputString)使用输入字符串作为参数调用方法,它将为您提供自动链接版本
完整代码:
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)
Run Code Online (Sandbox Code Playgroud)
如果输出linkedString的内容,则会得到以下输出:
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2760 次 |
| 最近记录: |