RegEx to find URLs and turn them into hyperlinks

 
$string = preg_replace("/(http:\/\/)?([a-zA-Z0-9\-.]+\.[a-zA-Z0-9\-]+([\/]([a-zA-Z0-9_\/\-.?&%=+])*)*)/", '<a href="http://$2">$2</a>', $string);

That little gem right there'll take any length of text and turn the urls in it into hyperlinks. Cool or what? :-)



Posted in PHP, Web development at July 13th, 2008. Trackback URI: trackback
Tags: ,

No Responses to “RegEx to find URLs and turn them into hyperlinks”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>