- 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 [997], Last Updated: Sat May 18, 2024
webmaster
Thu Dec 21, 2006
21 Comments
16967 Visits
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.com2.
http://www.wallpaperama.comso 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 "Howto" 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"><< 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