I wanted to implement a url matching algorithm that detects every url which starts with http or https. I come up with a solution but I am not sure whether it is enough to consider only spaces and control characters
function func($str){
$url = '/((http|https)[^[:space:][:cntrl:]]+)/i';
$str = nl2br(htmlspecialchars($str));
return preg_replace($url, '<a href="$1" target="_blank">$1</a>', $str);
}