I'd like to make URLs inside a text output clickable without having to escape the whole output. First I wanted to do it like this, but I would have to escape all of the output which is a security risk.
function formatContent($content)
{
$url_filter_protocol = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$url_filter_www = "/(www)\.[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
if (preg_match($url_filter_protocol, $content, $url)) {
return preg_replace($url_filter_protocol, "<a href='$url[0]' target='_blank'>$url[0]</a> ", $content);
} elseif (preg_match($url_filter_www, $content, $url)) {
return preg_replace($url_filter_www, "<a href='https://$url[0]' target='_blank'>$url[0]</a> ", $content);
} else {
return $content;
}
}
{!! nl2br(formatContent($post->content)) !!}
Does anybody know, how I could convert URLs in strings without having to escape to whole output?