Monday, January 11, 2010

Create Auto Hyperlink with PHP Script

Intrigued by the question from my friend, how do I create a script to automatically display links in the post article if there is a URL using PHP? For example, suppose I wrote the following string in the form:

"Hello .. always visit to get info about PHP programming"

Well .. once submitted, it will automatically text '' contained in the above string will become a link automatically without our html tags to link <a href="…"> ... </ a> on URL. To more easily, may be more comfortable if I call it with 'auto hyperlink' okay...

If you are using Wordpress, then this hyperlink auto automatically occur on the comment. But, what if we want to make our own auto hyperlink?'s quite easy, so...keep reading this article until the end.

We can make the script auto hyperlink above by using PHP. What about the idea? The idea is the first step we separate or split the original string prior to the substring based on the character of spaces.

For example, suppose there is the following string:

" site is really cool."

If we split the string into the substring based on a space character, the substring will we get the following:

Substring to-0:
Substring to-1: site
Substring to-2: is
Substring to-3: really
Substring to-4: cool

Thus, in total there are 5 substring. Then, for each substring, we check that starts with 'http://'. If there substring that begins with 'http://' then the substring will be direplace with

<a href="substring">substring</a>

Thus, the result string after replacing process complete is

<a href=""></a> site is really cool

Therefore, the replacing process should not be applied to the only pieces of 'http://' substring.

You should understand now. Well we can create the PHP script as the implementation of the ideas above. Here is the script :


// original string
$string = "Example some site and http://";

// splitting process to substring
$break = explode(" ", $string);

// replacing process
for ($i=0; $i<=sizeof($break)-1; $i++)
if ((substr($break[$i], 0, 7) == 'http://') && ($break[$i] != 'http://'))
$string = str_replace($break[$i], "<a href='".$break[$i]."'>".$break[$i]."</a>", $string);

echo $string;

The results of the above script looks like below.

Example some site and http://

Quite easy right? The script above is only a sample script, where the string input not included from a form. Well .. You can modify the script above, so the input string will be included from a form. Just feel free to ask. If you like this tutorial please make a comment here :D

Related Posts by Categories


sandhioke on February 12, 2010 at 8:24 AM said...

kalau ngadepin php, kepala suka jadi pusing..heheheh
top markotop, pasti owner blog ini programmer web

Etisbew on February 23, 2010 at 2:26 PM said...

Offshore Outsourcing Software Development Company Etisbew
- We have got useful information from your blog, please keep posting such information in future.

Rinkesh on August 4, 2010 at 7:17 PM said...

I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.
We buy Any Car

SEO tools on December 26, 2011 at 7:19 PM said...

Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.

Post a Comment

Maybe this is what you looking for


Subscribe Now


IT Skill is not enough... Copyright © 2009 Designed by Bie Blogger Template