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 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); 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] 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. 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
Archived
This topic is now archived and is closed to further replies.