Berone Posted March 13, 2007 Share Posted March 13, 2007 I am trying to make it so that in a post... Whenever someone posts a link, it is activated... for example... Go to http://www.link.com to see my site would change to Go to <a href='http://www.link.com'>http://www.link.com</a> to see my site. I can't figure out how to do this, and have been looking through the php functions to find the right one to use... Can anyone help me? Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/ Share on other sites More sharing options...
Berone Posted March 13, 2007 Author Share Posted March 13, 2007 This site does it already, see how it automatically made my url a link? Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/#findComment-206258 Share on other sites More sharing options...
Lumio Posted March 13, 2007 Share Posted March 13, 2007 Try it with RegEx: <?php $content = 'Hello! Visit my link: http://www.blubb.com www.link.com www.ahoi.com'; $content = preg_replace('{(http://www\.[\w\d\.:/]+)}', '<a href="$1">$1</a>', $content); $content = preg_replace('{(?<!http://)(www\.[\w\d\.:/]+)}', '<a href="http://$1">$1</a>', $content); echo $content; ?> Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/#findComment-206262 Share on other sites More sharing options...
mbtaylor Posted March 13, 2007 Share Posted March 13, 2007 Heh I wrote a different one... <?php $url = "http://www.some-url.com"; $url = preg_replace ("%(http://www.?[A-Za-z-]*.?[A-Za-z-]{1,5})%", "<a href='$1' title='$1' />$1</a>", $url); print ($url); ?> I like your use of \w\d - you also need to allow for a hyphen in the character range though. Regular expressions rule Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/#findComment-206270 Share on other sites More sharing options...
Lumio Posted March 13, 2007 Share Posted March 13, 2007 but what if there's a link like www.foo.com ? Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/#findComment-206272 Share on other sites More sharing options...
mbtaylor Posted March 13, 2007 Share Posted March 13, 2007 <?php $url = "http://www.some-url.com"; $url = preg_replace ("%([http://]*[www]{3}.?[A-Za-z-]*.?[A-Za-z-]{1,5})%", "<a href='$1' title='$1' />$1</a>", $url); print ($url); ?> Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/#findComment-206279 Share on other sites More sharing options...
mbtaylor Posted March 13, 2007 Share Posted March 13, 2007 Actually this is a bit better: %((http://)?(www)\.?[A-Za-z-]*\.?[A-Za-z-]{1,5})% Link to comment https://forums.phpfreaks.com/topic/42511-automatic-url-activation-in-text-helpp-soo-hard/#findComment-206365 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.