Tags

,

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

Example :

<?php
$string = "This is my PHP blog https://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=”https://avinash6784.wordpress.com”>https://avinash6784.wordpress.com</a&gt;

Advertisements