Turn URLs into links

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Turn URLs into links

Post by Locust »

I have my expression, but how do I convert

Code: Select all

http://www.url.com
into

Code: Select all

<a href=http://www.url.com>http://www.url.com</a>
I had a look everywhere but only found how to unlink URLs which was slightly different.

Code: Select all

$smessage = preg_replace('#^\w+://(www\.)?\w+\.\w+#','What should I put in here?',$smessage);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Download phpbb. Look in bbcode.php for a function similar to make_clickable().
Locust
Forum Commoner
Posts: 31
Joined: Sat Jul 22, 2006 10:26 am

Post by Locust »

Thanks feyd.

For others:

Code: Select all

$url = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $url);
Post Reply