Kyle
Kyle

Reputation: 4376

Parse text and convert links

Does anybody know of a good library out there for taking a string of text and using regex (or whatever you want) to parse out any urls to links? For example:

Input: I found this great site called google.com. You can e-mail the webmaster at [email protected].

Output: I found this great site called <a href="http://google.com">google.com</a&gt;. You can e-mail the webmaster at <a href="mailto:[email protected]">[email protected]</a>.

Upvotes: 0

Views: 247

Answers (1)

Adam Shiemke
Adam Shiemke

Reputation: 3742

I'm not aware of any canned solutions, but you could whip up a quick script. I found some regex to pick out urls, see: http://flanders.co.nz/2009/11/08/a-good-url-regular-expression-repost/

And for email addresses, \b[A-Z0-9._%-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b .

From there, prefixing with mailto: or http:// and adding the link tags should be trivial (I'd use perl).

Upvotes: 1

Related Questions