How to create auto hyperlinks using PHP code

The following script will help you to create auto hyperlinks using php code.

our string :

$string = " this is blog of anil kumar panigrahi http://www.anillabs.com";

call the function like

addLink($string);
How to create auto hyperlinks using PHP code by Anil Kumar Panigrahi

How to create auto hyperlinks using PHP code by Anil Kumar Panigrahi

below code will be split into sting + hyperlink url.

function addLink($string){
$breakstring = explode(" ", $string);
for ($i=0; $i<=sizeof($breakstring)-1; $i++)
{
if ((substr($breakstring[$i], 0, 7) == 'http://') &&$breakstring[$i] != 'http://'))
$string = str_replace($breakstring[$i], "<a href="'.$breakstring[$i].'">".$breakstring[$i]."</a>", $string);
}
return $string;
}

Hope that it will be useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online

Contact : [email protected]

You may also like...

1 Response

  1. Stripz says:

    Where is the code 2 be pasted?

Leave a Reply

Your email address will not be published. Required fields are marked *