mpridham Posted January 10, 2009 Share Posted January 10, 2009 (Sorry for the double post. I just realized I posted it in the math forum by mistake) I have created a very simple blog application and would like to add the functionality of having hyperlinks automatically created when a url is entered. For instance, when I post a blog entry with something like "Go to www.foobar.com", I would like it to automatically add the anchor tags (like this site does.) Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/140234-automatic-hyperlinking/ Share on other sites More sharing options...
sKunKbad Posted January 10, 2009 Share Posted January 10, 2009 Just use some simple regex to scan posts for the links, and then replace them with the linkified version. preg_match() or preg_replace are probably good functions to look at. Quote Link to comment https://forums.phpfreaks.com/topic/140234-automatic-hyperlinking/#findComment-733798 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 [tex]never noticed this tag whats this do?[/tex] ^^ a personal test lol.. but besides that let me write up something but you have to study some regex its very neat /((?:www\.|https?:\/\/)[.a-z0-9;&?\/-]+?)/i thats really really simple but should do your job (havn't tested it) -------------- wowie that tex thing is sweet lol Quote Link to comment https://forums.phpfreaks.com/topic/140234-automatic-hyperlinking/#findComment-733865 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 <?php $text = "omg hey go to http://www.lovinglori.com/signatures.php www.google.com its so cool!"; function link($a,$b) { if ($a == 'http://' || $a == "https://") { return "<a href='{$b}'>$b</a>"; } else { return "<a href='http://{$b}'>$b</a>"; } } echo preg_replace("/((www\.|https?:\/\/)[.a-z0-9;&?\/-]+)/ie","link('\\2','\\1')",$text); ?> Quote Link to comment https://forums.phpfreaks.com/topic/140234-automatic-hyperlinking/#findComment-733893 Share on other sites More sharing options...
mpridham Posted January 10, 2009 Author Share Posted January 10, 2009 Thanks folks, Your suggestions are greatly appreciated. Should get me to where I want to go. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/140234-automatic-hyperlinking/#findComment-733935 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.