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? Quote Link to comment 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? Quote Link to comment 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; ?> Quote Link to comment 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 Quote Link to comment 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 ? Quote Link to comment 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); ?> Quote Link to comment 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})% Quote Link to comment 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.