Bhagwat Singh

PHP Developer Portfolio

Converting String URLs to Clickable Links with PHP

Recently I came across a problem, I needed to convert a plain text string, that could possibly have a URL in it, to a string with clickable URLs. This is possible with the right PHP code. I created a simple function that takes a parameter of a string and searches for a valid URL (with a regular expression) within it.

function fnConvertToLinks($sInput)
{
$sOutput = preg_replace(
“/(http:\/\/|ftp:\/\/|https:\/\/)([^\s,]*)/i”,
$1$2“,
$sInput
);
return $sOutput;
}

This entry was posted in PHP. Bookmark the permalink.

Site Nevigation