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?

 

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.