tags:

views:

78

answers:

1
http://stackoverflow.com

should become

<a href="http://stackoverflow.com"&gt;http://stackoverflow.com&lt;/a&gt;


EDIT: It would be great if anchor tags from the original string stayed intact.

+2  A: 

John Gruber just posted an interesting regex example to capture URLs: Daring Fireball regex to get URLs

To cut to the chase, the (liberal, long) pattern he chose was:

\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))

That posits to capture all of the following:

http://foo.com/blah_blah
http://foo.com/blah_blah/
(Something like http://foo.com/blah_blah)
http://foo.com/blah_blah_(wikipedia)
(Something like http://foo.com/blah_blah_(wikipedia))
http://foo.com/blah_blah.
http://foo.com/blah_blah/.
<http://foo.com/blah_blah&gt;
<http://foo.com/blah_blah/&gt;
http://foo.com/blah_blah,
http://www.example.com/wpstyle/?p=364.
http://✪df.ws/123
rdar://1234
rdar:/1234
http://userid:[email protected]:8080
http://[email protected]
http://[email protected]:8080
http://userid:[email protected]
http://example.com:8080 x-yojimbo-item://6303E4C1-xxxx-45A6-AB9D-3A908F59AE0E
message://%[email protected]%3e
http://➡.ws/䨹
www.➡.ws/䨹
<tag>http://example.com&lt;/tag&gt;
Just a www.example.com link.

So you'd use that pattern with something like preg_filter, and then iterate over the returned array of matches somehow. I guess. I hate regex.

Alex Mcp