Jump to content

Archived

This topic is now archived and is closed to further replies.

macgyver

Reg Exp to replace hyperlinks

Recommended Posts

Hi I'm after some help to convert web addresses in some text body to hyperlinks.

So a user may enter [a href=\"http://www.phpfreaks.com\" target=\"_blank\"]http://www.phpfreaks.com[/a] into a text area. When the text is then displayed I want to go through and convert these to hyperlinks. Ideally would do it if the user missed off the http:// and typed www.phpfreaks.com.

I guess I need a reg expression but I need pointing in the right direction

Cheers

Mac

Share this post


Link to post
Share on other sites
here's what I would use:

preg_replace("/(http\:\/\/[^[::space::]]+)/", "<a href=\"\\1\">\\1</a>", $text);

Share this post


Link to post
Share on other sites
Thanks for that. However, I couldn't get it to work. I managed to find the following which works like a dream.

[code]preg_replace("/[^\"'=]((http|ftp|https):\/\/[^\s\"']+)/i", "<a href=\"\\1\">\\1</a>", $text);[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.