开发者

How can I extract all URls/links from an email using Ruby on Rails?

I am building a bookmarking site. I want to extract all URIs/links from an email. My site is in using Ruby on Rails.

Ho开发者_如何学JAVAw can I extract all the URLs of received email content?


Ruby's built-in URI module does this already:

From the extract docs:

require "uri"

URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
# => ["http://foo.example.com/bla", "mailto:test@example.com"]


require 'uri'

text = %{"test
<a href="http://www.a.com/">http://www.a.com/</a>, and be sure
to check http://www.a.com/blog/. Email me at <a href="mailto:b@a.com">b@a.com</a>.}


END_CHARS = %{.,'?!:;}
p URI.extract(text, ['http']).collect { |u| END_CHARS.index(u[-1]) ? u.chop : u }

Source: http://www.java2s.com/Code/Ruby/Network/ExtractURL.htm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜