Jump to content

Automatic Url Activation in Text HELPP Soo HArd


Berone

Recommended Posts

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?

 

 

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;
?>

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 :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.