3

This situation has no question yet. I wan't to detect if a string contains any link, that is all combinations of http,ftp,https, and the www., etc

Basically I want to prevent a string to contain any link.

I currently use:

name.split(/\s+/).find_all { |u| u =~ /^https?:/ }).count

What would be the best way to prevent any links in strings?

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
Rubytastic
  • 15,001
  • 18
  • 87
  • 175
  • 1
    Are you talking about HTML link (a tags) or just a URL string? – MrYoshiji Oct 11 '13 at 17:40
  • by splitting on whitespace, then searching, you're not utilizing the power of regexes. This is better: `fail if name =~ /\bhttps?:/`, although that won't catch all the link cases. Idk what your requirements are -- does "www.example.com" count? How about "example.com"? if so, it's hopeless because there's too many domains out there and maintenance would be a nightmare – Nathan Wallace Oct 11 '13 at 17:43
  • you _can_ do this with regexen, but if your goal is to prevent users to display such links, they will _always_ find a way to circumvent that. Like : h t t p : // s t a c k o v e r f l o w . c o m – m_x Oct 11 '13 at 17:47
  • also, possible duplicate of http://stackoverflow.com/questions/700163/detecting-a-naughty-or-nice-url-or-link-in-a-text-string?rq=1 – m_x Oct 11 '13 at 17:49
  • @m_x yes my goal is to prevent any links in user submitted content. The best I can do – Rubytastic Oct 11 '13 at 18:48

1 Answers1

6

I ended up using:

errors.add(:name, "Your name cannot contain links") if name.match /\b(?:(?:mailto:\S+|(?:https?|ftp|file):\/\/)?(?:\w+\.)+[a-z]{2,6})\b/

Which works quite good in almost all situations. Hacked together from several sources online

Rubytastic
  • 15,001
  • 18
  • 87
  • 175