How to convert URLs in string into Links

I want to convert URL in $string into link, that is convert the URL http://avinash6784.wordpress.com to <a href=”http://avinash6784.wordpress.com”>http://avinash6784.wordpress.com</a>. For achive this I have written following helpful PHP code.

Example :

<?php
$string = "This is my PHP blog http://avinash6784.wordpress.com";
$output = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $string);
echo $output;
?>

// Output will display – This is my PHP blog <a href=”http://avinash6784.wordpress.com”>http://avinash6784.wordpress.com</a>

Leave a Reply