macgyver Posted June 13, 2006 Share Posted June 13, 2006 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 directionCheersMac Quote Link to comment https://forums.phpfreaks.com/topic/11849-reg-exp-to-replace-hyperlinks/ Share on other sites More sharing options...
joquius Posted June 13, 2006 Share Posted June 13, 2006 here's what I would use:preg_replace("/(http\:\/\/[^[::space::]]+)/", "<a href=\"\\1\">\\1</a>", $text); Quote Link to comment https://forums.phpfreaks.com/topic/11849-reg-exp-to-replace-hyperlinks/#findComment-44929 Share on other sites More sharing options...
macgyver Posted June 13, 2006 Author Share Posted June 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/11849-reg-exp-to-replace-hyperlinks/#findComment-44957 Share on other sites More sharing options...
joquius Posted June 13, 2006 Share Posted June 13, 2006 sorry mistake in the code shouldn't have put \: i suppose. Quote Link to comment https://forums.phpfreaks.com/topic/11849-reg-exp-to-replace-hyperlinks/#findComment-44958 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.