1

Is there a regex out there that can find a string that contains a word that starts with either http:// or www and wrap it with <a>$1</a>?

Been googling but I can't seem to find a ultimate one.

Another question, Could you somehow make it ignore it if its inside a <img> tag ?

Thanks a bunch!

2 Answers2

9
    $text = trim($text);
    while ($text != stripslashes($text)) { $text = stripslashes($text); }    
    $text = strip_tags($text,"<b><i><u>");
    $text = preg_replace("/(?<!http:\/\/)www\./","http://www.",$text);
    $text = preg_replace( "/((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>",$text);
Devin Ceartas
  • 4,743
  • 1
  • 20
  • 33
  • a snippet from a function I use often; also contains (not shown) code to replace email with links, optionally obfuscated – Devin Ceartas Aug 07 '09 at 03:36
1

Good luck with this one — finding the beginning is fairly easy (most of the time); finding the end? Good luck:

  • http://example.com/bob.jones.4.
  • http://example.com/bob.jones.4?
  • http://example.com/bob.jones.4!
  • http://stackoverflow.com/questions/1242733/make-links(oh-noes)
  • http://example.com/bob.'magic'.jones*2!
  • http://example.com/~(*)!

Those are valid URLs. See RFC2396. But sometimes you want the trailing punctuation, sometimes you don't.

/me wonders what he can use a url with (*')! in it for, now that he knows its permitted by RFC2396.

derobert
  • 49,731
  • 15
  • 94
  • 124
  • What's the problem using all characters in a string? If it before escape them with functions like htmlentites all is goo, isnt it? – kwichz May 09 '11 at 14:33
  • @kwichz: The problem is that in a plan text string, its easy to find http://, but its difficult to find the *end* of the URL, due to all the characters that are allowed in a URL. For example: `He said to 'Try searching at http://www.google.com/.'` Both period and single-quote are valid characters in a URL, so its difficult for a program to see where that URL ends. – derobert May 09 '11 at 17:16
  • I fail to see the problem. Just match until you find the first space and it will work in most cases. – Maciej Swic May 08 '13 at 08:59
  • @MaciejSwic `Well, that fails on the common convention of wrapping URLs in punctuation (e.g., ). Or on URLs at ends of sentences, like http://www.example.com/page1.html. You can get a 90% solution, as you say, but should be aware of how it'll fail.` (Comment wrapped in \` to prevent SO from messing it up...) – derobert May 08 '13 at 15:58