How To Make Clickable Text URL Links From Text Links Change To Clicking

Forums » PHP » How To Make Clickable Text URL Links From Text Links Change To Clicking
this page will show you how to make clickable text urls from simple text links using php. i belive is a function you can use in your scripts its open source so is writte for you to try it. i will also give you example code you can test for yourself
today i will teach you how to make clickable links from ordinary text.
for example, lets say someone wrote this article in your site like your blog, forums or guestbook.

--------- start of article

Hi everyone, i am writing this short article to show you how you can make ordinary text into clickable link. In my example, i will make www.wallpaperama.com and/or http://www.wallpaperama.com into links that people can click to when they post topics, articles, posts, blogs etc..

--------- end of article

ok, in my above example, i have two urls i can make into clickable links:

1. www.wallpaperama.com
2. http://www.wallpaperama.com

so after you have applied the this functions below, the example article will have two link and it should look like this:

--------- start of article with clickable links

Hi everyone, i am writing this short article to show you how you can make ordinary text into clickable link. In my example, i will make www.wallpaperama.com and/or http://www.wallpaperama.com into links that people can click to when they post topics, articles, posts, blogs etc..

--------- end of article with clickable links

for this PHP script to work, you must have PHP hosting, if you don't have PHP hosting on your website, you can contact our friends at www.webune.com to sign up with one of their PHP plans.

now to make it work. the first step is to copy and paste the following code into your text editor, you can use notepad in windows if you like.

CODE:
<?

function clickable_link($text)
{
# this functions deserves credit to the fine folks at phpbb.com

   $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);

   // pad it with a space so we can match things at the start of the 1st line.
   $ret = ' ' . $text;

   // matches an "xxxx://yyyy" URL at the start of a line, or after a space.
   // xxxx can only be alpha characters.
   // yyyy is anything up to the first space, newline, comma, double quote or <
   $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);

   // matches a "www|ftp.xxxx.yyyy[/zzzz]" kinda lazy URL thing
   // Must contain at least 2 dots. xxxx contains either alphanum, or "-"
   // zzzz is optional.. will contain everything up to the first space, newline,
   // comma, double quote or <.
   $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);

   // matches an email@domain type address at the start of a line, or after a space.
   // Note: Only the followed chars are valid; alphanums, "-", "_" and or ".".
   $ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $ret);

   // Remove our padding..
   $ret = substr($ret, 1);
   return $ret;
   }
$text="www.wallpaperama.com";
   
?>
<HTML>
<TITLE>Making Text URLs Into Clicable Links Tutorial</TITLE>
<HEAD>
<style type="text/css">
<!--
.style2 {color: #0066FF}
-->
</style>
</HEAD>
<BODY>
<h1 align="center" class="style2">Making Text URLs Into Clicable Links Tutorial</h1>
<p>the clickable link is before function is: <strong><?php echo $text; ?></strong></p>
<p>this is the text after the <span class="style2">clickable_link_function</span>: <?php echo clickable_link($text); ?> </p>
<hr size="2" noshade>
 <p> If you have benefited or learned from this short &quot;Howto&quot; tutorial, we would appreciate if you can provide a link to our http://www.wallpaperama.com. By linking to Wallpaperama, you will make these tutorials more available to others who are looking for information like this.</p>
 <p>Thank You</p>
 <p>Wallpaperama Team</p>
 <p><a href="http://www.wallpaperama.com/forums">&lt;&lt; Go Back</a> </p>
<p align="center">PHP Hosting by <a href="http://www.webune.com">Webune.com</a> </p>
<p>
</BODY>
</HTML>


now save this file as clickable-link.php and upload to your website, then open it with your browser and it will show you it works. you can also look at the code to see for yourself how the script works.

Our friends at www.webune.com have provide us with PHP hosting to show you this script in action just incase you don't have PHP but want to learn about PHP programming.

if you've learned from this howto tutorial, we would appriciate a link black to http://www.wallpaperama.com. by linking to us, you will make this tutorial guide more available to other who are looking for free information like this.

Thanks

Wallpaperama Team
sty
good stuff!
i am trying to make it tell if it is a youtube video link and then create the right link code around it so it is embedded. any help?
sty
it is just converting the first link. any ideas on how to make it convert all?
sty
test on 3 links to see if it is my copy or implementation of the code:

google
slashdot.org
foofoo
tomas
hello,
how can i replace \\2 to a shorter url using substr?
substr("\\2", 0, 10);
how can i do this? please can you help me with it?

please post something here...
me
you need to use the \ character
tomas
i tried

substr("\", 0, 5);

doesnt work... \ just ignores what php sayd to do... can you make any example?
Stijn
hi, great script! but i want also the possibillity to choose your text that appears on the screen. does anybody has a solution for this?
Sandra
i have a question... i don't know the correct terms or names, so bare with me. i want to take a picture link from photobucket, either the hmlt or the img..... preferably the img link and make a link to in without using the whole address location. i just want it to say "click here" or the name of the subject.

i want to post on craig's list but i just want a "click here" type of link.

how do i make that?

thanks
sandra
sharika
hey hello love yuo
Ali
hey thanks for great script - i am struggling with finding a way to truncate the display text of the link (very long links break my css) whilst leaving the url intact in the link - can regex do this? my regex is rubbish otherwise i guess i wouldn't be here ;)
Qaseem
online buying and selling on internet
in all pakistan.
Juan
this works like a charm. great code!
Chris
that's very good stuff,
thanks friend ;)
zeb
thanks for this awesome code!
miklos
Good little function, thanks for posting! Miklos mikl
michael
i don't quite comprehend, please expatiate further.
rtretret
ertertetertert ret retret erter tertret ert erter t ert ert er er ert
Dan
accounts for being first item in line, but fails with <p>domain dot com is a domain</p>
Naveen
Nice post, visit my bog
maryann
I clicked here to see demo and did not see a demo. Not helpful
Title: How To Make Clickable Text URL Links From Text Links Change To Clicking [997]
Tags: how to make clickable text url links from text links change to clicking
By
Comments 21
Visits 16798