Text to Links

Replace any ‘http://’ text URLs with clickable hyperlink that open in a new window:

<?php
function textToHyperlinks( $string ) {
	return preg_replace(
		'@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@',
		'<a href="$1" target="_blank">$1</a>',
		$string
	);
}

Stack Overflow