URL pattern for wrapping links

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

Moderator: General Moderators

Post Reply
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

URL pattern for wrapping links

Post by Eran »

I'm looking for a pattern that can match URL addresses in order to wrap them with an anchor tag. Something like:

Code: Select all

preg_replace($pattern, '<a href="$1">$1</a>', $text);
Where the pattern could match any simple URL address, with or without the protocol. Somehow I'd figured I could google my way to this, but have been failing miserably.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: URL pattern for wrapping links

Post by GeertDD »

Here is an auto_link_urls() function I wrote: http://dev.kohanaphp.com/browser/trunk/ ... t.php#L243
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: URL pattern for wrapping links

Post by omniuni »

Nice! I bookmarked that!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: URL pattern for wrapping links

Post by Eran »

Thanks a lot! just what I was looking for :)
Post Reply